android intent filter for custom file extension

2020-02-13 04:47发布

I am trying to make an android app to open files with extension .abc and this is my application section from android manifest xml

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name="com.example.app.ActName" android:label="AppName">
        <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:mimeType="image/jpeg"/>
        </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=".*\\.abc" />
            <data android:host="*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

I used all the experience from the most popular and related questions

Android intent filter for a particular file extension?

Android intent filter: associate app with file extension

Android How to create Intent Filter for custom file extension that does NOT make it part of a chooser for everything on the phone

And in the end this does not work - when I click on a file named "name.abc" in ES file browser it asks me if i want to open the file as text or image etc. when it actually supposed to just open the file in my app instead

What am i doing wrong? What is the proper intent filter to launch the app by just clicking a file with the corresponding extension in any file manager?

upd.

it looks like different android systems and different file browsers act in different way - one intent filter works just fine on 4.2 in "ES File Explorer" and on 4.0.4 in default file manager, but does not work at all on 4.2 and 4.0.4 in "Total Commander" and on 4.0.4 in "ES File Explorer"

1条回答
闹够了就滚
2楼-- · 2020-02-13 05:45

First of all, the first intent-filter in your example uses MIME type image/jpeg. Is jpeg your .abc extension?

I think there may be three reasons why this does not work:

  1. You need to add more MIME types. Try adding the following:

    <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:mimeType="application/abc"/>
    </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:mimeType="application/octet-stream"/>
    </intent-filter>
    

    And, if your .abc format is a text format:

    <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:mimeType="text/plain"/>
    </intent-filter>
    
  2. You have registered the file scheme. You might also need the content scheme:

    <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="content" />
        <data android:mimeType="*/*" />
        <data android:pathPattern=".*\\.abc" />
        <data android:host="*" />
    </intent-filter>
    
  3. Rumor has it that pathPattern with a dot only matches files containing a single dot. In your example, this would mean that your app would be associated with One.abc but not with Two.Dots.abc. Try adding more pathPatterns .*\\..*\\.abc, .*\\..*\\..*\\.abc etc.:

    <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=".*\\..*\\.abc" />
        <data android:host="*" />
    </intent-filter>
    
查看更多
登录 后发表回答