Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android intent filter for custom file extension

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...