意图过滤器以捕获查看 *.ext 文件的意图

intent-filter to catch intents for viewing *.ext files

我正在尝试找出我需要的意图过滤器,以便:

查看扩展名为 *.npk 的文件或内容

我阅读了大量关于 intent-filters 的 Whosebug 文章,但仍然不明白我错过了什么。

例如,当这是我的意图过滤器时,我希望它能够捕获扩展名为“*.npk”的文件。我知道模式的错误,所以我添加了多条数据线以捕获 .npk 之前有 0-4 个点的路径:

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

当我从 total commander 应用程序打开一个文件时,它可以正常工作,并按预期启动我的 activity(顺便说一句,即使我只有一个 pathPattern=".*\\.npk",它也能正常工作,所以也许提到的错误已在 lollipop 中修复):

{act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/com.fletech.smartbaby.android.pro/files/npk/animal-water-he.npk typ=application/octet-stream flg=0x10000000 cmp=com.fletech.smartbaby.android/.CategorySliderActivity}

但是我无法在 Dropbox 应用程序中使用它。这是 logcat 的 "captured" 意图。为了捕获它,我添加了 android:mimeType="*/*" 这样我就可以选择我的应用程序,现在每个文件(也是 .jpg)都想打开我的应用程序。

{act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/com.dropbox.android/files/u123456/scratch/apk/nature_0.npk typ=application/octet-stream flg=0x10000003 cmp=com.fletech.smartbaby.android/.CategorySliderActivity (has extras)}

我不明白为什么上面的过滤器没有捕捉到这个意图。我在总指挥官意图和 Dropbox 意图之间看到的唯一区别是标志,不应该影响恕我直言的额外内容,并且文件扩展名之前的路径中有 2 对 4 个点,但我的意图过滤器应该小心

注意:我正在 lollipop 上开发和测试,但我希望它能在 api 9+ 上运行。

经过长时间的反复试验,我发现以下解决方案有效。我必须将 android:host="*" 和 android:mimeType="*/*" 添加到数据元素中。官方android文档中似乎有些不一致。

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