一个活动如何匹配多个文件扩展名或MIME类型的intent-filter?

9

先生们,

我正在尝试使我的Android应用程序能够响应文件的打开(通过匹配它们的扩展名)和MIME类型(因此它们将可以从浏览器中使用)。

我已经按照这里的建议进行了操作:

Android intent filter for a particular file extension?

但仍然没有成功。

我的Android清单文件中相关的部分如下所示:

<activity android:name="MuPDFActivity"
              android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/vnd.ms-xpsdocument"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/pdf"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/x-cbz"/>
        </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:scheme="file"/>
            <data android:mimeType="*/*"/>
            <data android:pathPattern=".*\\.xps"/>
            <data android:host="*"/>
        </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:scheme="file"/>
            <data android:mimeType="*/*"/>
            <data android:pathPattern=".*\\.pdf"/>
            <data android:host="*"/>
        </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:scheme="file"/>
            <data android:mimeType="*/*"/>
            <data android:pathPattern=".*\\.cbz"/>
            <data android:host="*"/>
        </intent-filter>
    </activity>

正如您所见,我希望该应用程序能够针对.pdf、.xps和.cbz文件以及相关的MIME类型进行调用。本地测试表明,.pdf和application/pdf部分都可以工作,但无论我怎么尝试,.xps(以及可能的.cbz)部分都不能工作。
我是否遗漏了一些明显的东西?每个Activity只能有一个MIME类型/文件模式吗?
非常感谢任何帮助。
谢谢,
罗宾

哪个安卓版本?我注意到这里有一些差异。请查看我的问题:http://stackoverflow.com/questions/20650378/how-did-intent-filter-change-from-android-2-to-android-4 - Martin
3个回答

4
据我所知,那应该是这样的(一个过滤器有多个值):
 <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.ms-xpsdocument"/>
        <data android:mimeType="application/pdf"/>
 </intent-filter>

此外,mime类型是否有误可能吗?

有趣。我曾经尝试过将android:mimeTypeandroid:pathPattern结合起来使用,但并没有成功。 - Martin
对于 android:pathPattern,在 Android 4 中需要设置 <data> 标签的所有四个属性。因此,您的提示对 OP 没有帮助。 - Martin

3

多个 <data> 之间是逻辑 OR 并且被单独处理。因此,您可以有一个带有android:scheme但没有android:pathPattern的标签,以及一个带有android:pathPattern但没有android:host的标签等等。因此,没有任何一个<data>标签是完整的,也不会起到任何作用。

您应该使用:

    <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:scheme="file"
          android:mimeType="*/*"
          android:pathPattern=".*\\.cbz"
          android:host="*"
        ></data>
    </intent-filter>

您可以有第二个<data>,但如果您想使用android:pathPattern,则它需要再次具有所有四个属性,因为在Android 4中,所有四个属性都是强制性的。(在较旧的Android版本中不是这样)


嗨,马丁,如果我使用上面的意图过滤器,当我尝试从通知中打开 Gmail 应用时,我的应用程序会显示出来。 - madan V
多个<data>标签组合起来可以使用吗?对我来说它只能检测到第一个。 - IgorGanapolsky

0
每个Activity只能有一个MIME类型/文件模式吗?
不是的。根据您自己的说法,两个正在工作(PDF x 2)。
我在这里错过了什么明显的东西吗?
我怀疑元素对您拥有它的那些元素没有太大帮助,并且您需要将其用于其他元素。适用于浏览器,它将沿着MIME类型路径进行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接