Android intent filter for a particular file extens

2018-12-31 18:31发布

I want to be able to download a file with a particular extension from the 'net, and have it passed to my application to deal with it, but I haven't been able to figure out the intent filter. The filetype is not included in the mimetypes, and I tried using

<data android:path="*.ext" />

but I couldn't get that to work.

13条回答
唯独是你
2楼-- · 2018-12-31 19:21

When an Intent meets a intent-filter, these are the intent-filter requirements: (imagine a checklist).

  • Any matching <action>
  • Any matching <category>
  • Any matching <data mimeType> (easy fix: "/")
  • Optionally:

    • Any matching <data scheme> (easy fix: <data android:scheme="file" /> <data android:scheme="content" />)

    • Any matching <data host> (easy fix: "*")

    • Any matching <data pathPattern/etc.> (for example .*\\.0cc)

Defining multiple <data $type=""> elements checks the $type box iff any <data $type=> matches the Intent.

Omitting mimeType breaks your intent-filter, even though it's seemingly redundant. Omitting <data scheme/host/pathPattern> causes your filter to match everything.

https://f-droid.org/en/packages/de.k3b.android.intentintercept/ is an app designed to receive all intents, and allows you to inspect the intent. I learned that unrecognized file extensions opened via Simple File Manager are delivered with MIME type application/octet-stream.

https://stackoverflow.com/a/4621284/2683842 reports that <data pathPattern=> .*xyz aborts at the first x it sees, and will fail immediately if not followed by yz. So /sdcard/.hidden/foo.0cc will not pass .*\\.0cc unless you try .*\\..*\\.0cc instead.

  • I did not verify whether this workaround is necessary.

End result:

<activity android:name=".Ft2NsfActivity">

    <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:scheme="content" />
        <data android:host="*" />
        <data android:pathPattern=".*\\.ftm"/>
        <data android:pathPattern=".*\\..*\\.ftm"/>
        <data android:pathPattern=".*\\..*\\..*\\.ftm"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\.ftm"/>
        <data android:pathPattern=".*\\.0cc"/>
        <data android:pathPattern=".*\\..*\\.0cc"/>
        <data android:pathPattern=".*\\..*\\..*\\.0cc"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\.0cc"/>
        <data android:mimeType="*/*" />
    </intent-filter>

</activity>
查看更多
登录 后发表回答