Android intent filter: associate app with file extension Android intent filter: associate app with file extension android android

Android intent filter: associate app with file extension


You need multiple intent filters to address different situation you want to handle.

Example 1, handle http requests without mimetypes:

  <intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.BROWSABLE" />    <category android:name="android.intent.category.DEFAULT" />    <data android:scheme="http" />    <data android:host="*" />    <data android:pathPattern=".*\\.pdf" />  </intent-filter>

Handle with mimetypes, where the suffix is irrelevant:

  <intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.BROWSABLE" />    <category android:name="android.intent.category.DEFAULT" />    <data android:scheme="http" />    <data android:host="*" />    <data android:mimeType="application/pdf" />  </intent-filter>

Handle intent from a file browser app:

  <intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <data android:scheme="file" />    <data android:host="*" />    <data android:pathPattern=".*\\.pdf" />  </intent-filter>


The other solutions did not work reliably for me until I added:

android:mimeType="*/*" 

Before that it worked in some applications, in some not...

complete solution for me:

<intent-filter>  <action android:name="android.intent.action.VIEW" />  <category android:name="android.intent.category.DEFAULT" />  <data android:scheme="file"  android:host="*" android:pathPattern=".*\\.EXT" android:mimeType="*/*"  /></intent-filter>


The answeres given by Phyrum Tea and yuku are very informative already.

I want to add that starting with Android 7.0 Nougat there is a change to the way file sharing between apps is handled:

From official Android 7.0 Changes:

For apps targeting Android 7.0, the Android framework enforces the StrictMode API policy that prohibits exposing file:// URIs outside your app. If an intent containing a file URI leaves your app, the app fails with a FileUriExposedException exception.

To share files between applications, you should send a content:// URI and grant a temporary access permission on the URI. The easiest way to grant this permission is by using the FileProvider class. For more information on permissions and sharing files, see Sharing Files.

If you have your own custom file ending without a specific mime-type (or i guess even with one) you may have to add a second scheme value to your intent-filter to make it work with FileProviders too.

Example:

<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:mimeType="*/*" />    <!--        Work around Android's ugly primitive PatternMatcher        implementation that can't cope with finding a . early in        the path unless it's explicitly matched.    -->    <data android:host="*" />    <data android:pathPattern=".*\\.sfx" />    <data android:pathPattern=".*\\..*\\.sfx" />    <data android:pathPattern=".*\\..*\\..*\\.sfx" />    <data android:pathPattern=".*\\..*\\..*\\..*\\.sfx" />    <!-- keep going if you need more --></intent-filter>

The important thing here is the addition of

<data android:scheme="content" />

to the filter.

I had a hard time finding out about this little change which kept my activity from opening on Android 7.0 devices while everything was fine on older versions. I hope it helps someone.