【问题标题】:Associating App with Epub format将 App 与 Epub 格式相关联
【发布时间】:2015-02-04 18:11:29
【问题描述】:

我无法让我的应用程序在移动设备上注册 epub 文件。我的 android 清单中有一组意图过滤器,但它仍然无法使用 sd 卡上的 epub 文件打开。当我浏览File Explorer 应用程序时,它会显示该文件,但是当我单击它时,它会显示“系统不支持这种类型的文件:”。当我从 Internet 下载文件,然后使用下载应用程序导航到下载文件夹时,该文件根本不显示(即使它位于文件浏览器的下载文件夹中)。我还尝试让 epub 文件显示文件选择器意图 (Intent.ACTION_OPEN_DOCUMENT),但没有运气。我猜最后两个没有出现,因为意图加载了Intent.CATEGORY_OPENABLE

我尝试了多个 epub 文件,但都没有成功。

有人可以帮我弄清楚我错过了什么吗?

使用 KitKat 和更高版本的手机。

注意:这确实适用于从互联网下载。如果我转到 epub 链接,这可行,但不是来自文件系统。

        <!-- Open File Types -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:host="*" android:scheme="file"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:host="*" android:scheme="file" android:mimeType="text/plain"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="file"/>
            <data android:mimeType="application/epub+zip"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="http"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="http" android:mimeType="text/plain"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="http"/>
            <data android:mimeType="application/epub+zip"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="https"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="https" android:mimeType="text/plain"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="https"/>
            <data android:mimeType="application/epub+zip"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="content"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="content" android:mimeType="text/plain"/>
            <data android:pathPattern=".*\\.epub"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="content"/>
            <data android:mimeType="application/epub+zip"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:scheme="book"/>
        </intent-filter>

        <intent-filter
            android:icon="@raw/icon"
            android:label="ePub File"
            android:priority="1" >
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="ftp" />
            <data android:scheme="file" />
            <data android:host="*" />
            <data android:mimeType="*/*" />
            <data android:pathPattern=".*\\.epub" />
        </intent-filter>

更新:

为赏金寻找以下问题的答案:

  1. 如何让 android 系统识别可以使用我的应用打开内部或外部存储上的 epub 文件?
  2. 如何让默认文件浏览器 (Storage Access Framework) 显示 epub 文件?

【问题讨论】:

  • 第一个 intent-filter(没有检查其他)在 Nexus 5(Android 5.0)上运行良好。愚蠢的问题,但是您确定要在活动标签中定义过滤器吗? :)
  • 看看这个,一定有帮助:stackoverflow.com/questions/3760276/…
  • @ozbek,是的,该应用程序只有一个活动,这些活动都在其中。感谢您的检查!
  • @ozbek,你说它适用于 Nexus 5?你能解释一下你打开文件做了什么吗?
  • 简单地创建了一个示例应用程序,其中包含第一个意图过滤器和一个在目标内部 SD 卡中扩展名为 .epub 的空文件。然后尝试使用OI File Manager 打开文件。系统显示“打开方式...”对话框,其中测试应用程序也被列为选项。

标签: android android-intent epub file-association


【解决方案1】:

如何让 android 系统识别可以使用我的应用打开内部或外部存储上的 epub 文件?

“android 系统”与它没有太大关系,尤其是在今天。

.epub/application/epub+zipMimeTypeMap(或更准确地说,来自框架类的libcore.net.MimeUtils)的支持是added ~35 hours ago。据推测,它将出现在未来的 Android 版本中。在此之前,唯一会使用该 MIME 类型的文件管理器是那些自己添加它的文件管理器。

在高层次上,当遇到这样的问题时,解决方案相当简单:

  1. 找到另一个可以满足您需求的应用程序(在本例中是另一个 EPUB 阅读器)

  2. 使用the App Browser app 查看该应用的清单是什么样的,以及它为&lt;intent-filter&gt; 节选择了什么

一般来说,我通常会看到&lt;intent-filter&gt; 带有一个方案和一个 MIME 类型一个方案、主机和路径的东西。拥有 MIME 类型 路径的东西不太可能有帮助,就好像 Intent 没有明确地在其中包含 MIME 类型,并且 Android 不知道将该特定扩展名映射到您的 MIME输入,您的&lt;intent-filter&gt; 可能不匹配。

此外,您将需要使用多个“文件管理器”应用程序进行测试,因为 Android 没有文件管理器,因此您可能会在您正在测试的应用程序中遇到错误/限制。

如何让默认文件浏览器(存储访问框架)显示 epub 文件?

指定正确的 MIME 类型并祈求奇迹。

同样,在 Android 本身提供更多内置支持以将 .epub 映射到 MIME 类型之前,您必须依赖存储提供商自己碰巧知道 .epub 映射到 application/epub+zip MIME 类型。一些提供商会这样做,因为他们从某个后端服务器获取信息,这些服务器可能比 Android 本身了解更多的 MIME 类型。某些提供程序可能没有,例如 Android 的 MediaStore 支持的外部存储提供程序,因为我怀疑 MediaStore 是否有自己对 EPUB 文件的本地支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2023-04-08
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多