Android intent filter for custom file extension

I am trying to create an android application to open files with the extension .abc and this is the section of my application 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 specific file extension?

Android intent filter: associate an application with a file extension

Android How to create an Intent filter to expand a user file that DOES NOT make it part of the choice for everything on the phone

And in the end, it won’t work - when I click on a file called "name.abc" in the ES file browser, it asks me if I want to open the file as text or image, etc., when it actually should have just open the file in my application instead

What am I doing wrong? What is the correct intent filter to run the application by simply clicking on the file with the appropriate extension in any file manager?

update

It seems that different Android systems and different file browsers act differently - one filter design works fine on 4.2 in "ES File Explorer" and on 4.0.4 in the default file manager, but does not work at all on 4.2 and 4.0.4 in "Total Commander" and 4.0.4 in "ES File Explorer"

+1
source share
1 answer

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

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

  • 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> 
  • You have registered the file scheme. You may also need a content schema:

     <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> 
  • Hearing shows that pathPattern with a point matches only files containing one point. In your example, this means that your application will be associated with One.abc , but not with Two.Dots.abc . Try adding additional 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> 
+2
source

Source: https://habr.com/ru/post/952998/


All Articles