Multiple file extension/mimetype intent

Gents,

I'm trying to get it so that my Android application can respond both to files being opened (via matching their extensions) and to mime-types (so they will work from the browser).

I've followed the advice here:

Android intent filter for a particular file extension?

but still had no luck.

The relevant section from my android manifest file is as follows:

<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>

As you can see, I would like the app to be invoked for .pdf, .xps, and .cbz files, also files with the relevant mimetypes. Local tests here seem to suggest that the .pdf and application/pdf sections are both working, but try as I might, the .xps (and presumably .cbz) sections are not.

Am I missing something obvious here? Can each Activity only have one mimetype/file pattern?

Any help would be much appreciated.

Thanks,

Robin


afaik, that would rather be like that (one filter with the various values):

 <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>

Also, is it possible that the mime-type is incorrect?


Multiple <data> are logical OR and treated separately. So you have one tag with android:scheme but without android:pathPattern and one with android:pathPattern but without a android:host and so on. So none of the <data> tags are complete and will do any good.

You should use:

    <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>

You can have a second <data> but it would need all four attributes again as all four attributes are mandatory with Android 4 if you want to use android:pathPattern . (They weren't with older Android versions)


Can each Activity only have one mimetype/file pattern?

No. By your own admission, two are working (PDF x 2).

Am I missing something obvious here?

I doubt that the BROWSABLE element will do you much good on the ones you have it on, and you need it for the others. BROWSABLE is for browsers, which will go down the MIME type path.

链接地址: http://www.djcxy.com/p/57360.html

上一篇: 使用xmlwriter附加xml文件

下一篇: 多文件扩展名/ mimetype意图