I have a custom file type/extension that I want to associate my app with.
As far as I know, the data element is made for this purpose, but I can't get it working. http://developer.android.com/guide/topics/manifest/data-element.html According to the docs, and a lot of forum posts, it should work like this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" />
</intent-filter>
Well, it does not work. What did I do wrong? I simply want to declare my own file type.
Notice that this will get your app open all gmail file attachments, there is no way to work around it
The
pathPattern
does not work if the file path contains one or more dots before ".pdf".
This will work:
Add more if you want to support more dots.
Markus Ressel is correct. Android 7.0 Nougat no longer permits file sharing between apps using a file URI. A content URI must be used. However, a content URI does not allow a file path to be shared, only a mime type. So you cannot use a content URI to associate your app with your own file extension.
Drobpox has an interesting behavior on Android 7.0. When it meets an unknown file extension it appears to form a file URI intent but instead of launching the intent it calls the operating system to find out which apps can accept the intent. If there is only one app that can accept that file URI it then sends an explicit content URI directly to that app. So to work with Dropbox you do not need to change the intent filters on your app. It does not require a content URI intent filter. Just make sure the app can receive a content URI and your app with your own file extension will work with Dropbox just like it did before Android 7.0.
Here is an example of my file loading code modified to accept a content URI:
The other solutions did not work reliably for me until I added:
Before that it worked in some applications, in some not...
complete solution for me:
My findings:
You need several filters to deal with the different ways of retrieving a file. ie, by gmail attachment, by file explorer, by HTTP, by FTP... They all send very different intents.
And you need to filter out the intent that trigger your activity in your activity code.
For the example below, I created a fake file type new.mrz. And I retrieved it from gmail attachment and file explorer.
Activity code added in the onCreate():
Gmail attachement filter:
File explorer filter:
HTTP filter:
Private functions used above:
Content URI ftw, and with the intent filter in the manifest... if your files have a custom extension .xyz, add a matching mime type:
Some apps such as email seem to convert the extension into a mime type. Now I can click on the attachment in email and have it open in my app.