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

I saw numerous answers here about creating an intent filter for a custom file extension, but none of them seem to answer my question:

I have an intent filter that works right now ... when I view a file or open it from an email attachment, my application will appear in the list. The file itself has its own extension "tgtp", but basically it is just an xml file.

The problem I am facing is that although this intent filter works, it also adds my application to every selection item for every type of file on my phone. For example, if I delete the default settings for my contacts and click one of my contacts, it says that my application can open it.

I tried dozens of different combinations of intent filters with different schemes, mime types, etc ... and some still opened the file if I was viewing a file browser, but I need to be able to open email attachments and open as a file browser. I have not yet found an intent filter that allows me to do this without making my application available to all other choices.

Here is my current intent filter, which my application uses to open everything:

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

Thank you in advance

+8
android eclipse email intentfilter
source share
5 answers

The only way to solve this problem is to add the scheme and host attributes to your intent filter:

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

This is because the documentation says that android:pathPattern only works if a schema and host are defined. http://developer.android.com/guide/topics/manifest/data-element.html

Hope this helps.

+14
source share

I struggled with this so much for my own file extension. After much searching, I found this web page where the poster found that the Android class patternMatcher (which is used to map PathPattern in Intent-Filters) has unexpected behavior when your path contains the first character of your match pattern elsewhere in the path (for example, if you are trying to match "* .xyz", the patternMatcher class stops if "x" is in your path). Here is what he found for a workaround and worked for me, although this is a little hack:

PatternMatcher is used for PathPattern in IntentFilter. But, the PatternMatcher algorithm is rather strange for me. Here is the Android PatternMatcher algorithm.

If there is a โ€œnext characterโ€ of the pattern โ€œ. *โ€ In the middle of the line, PatternMatcher stops the loop at that point. (See PatternMatcher.java Android.)

Ex. string: template "this my attachment": ".att.". Android PatternMatcher introduces a loop to match '.' pattern to meet the following character pattern (in this example, 'a') So, '.' the matching loop stops at index 8 - 'a' between 'is' and 'my'. Therefore, the result of this match returns false.

Pretty strange, isn't it. To get around this - actually reduce the chance - the developer should use the annoying stupid pathPattern.

Ex. Purpose: Matching a uri path that includes a "message".

 <intent-filter> ... <data android:pathPattern=".*message.*" /> <data android:pathPattern=".*m.*message.*" /> <data android:pathPattern=".*m.*m.*message.*" /> <data android:pathPattern=".*m.*m.*m.*message.*" /> <data android:pathPattern=".*m.*m.*m.*m.*message.*" /> ... </intent-filter> 

This is especially true when comparing with the user file extension.

+3
source share

Put this intent filter in the action you want to open by touching the file:

 <intent-filter android:priority="999"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.OPENABLE" /> <data android:host="*" /> <data android:mimeType="application/octet-stream" /> <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\.yourextension" /> <data android:pathPattern=".*\\.yourextension" /> <data android:scheme="content" /> </intent-filter> 
0
source share

One possible answer is shown here . Basically, try the following intent filter inside the desired activity tag to open:

 <intent-filter android:priority="999"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.OPENABLE" /> <data android:host="*" /> <data android:mimeType="application/octet-stream" /> <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\..*\\.yourextension" /> <data android:pathPattern=".*\\..*\\.yourextension" /> <data android:pathPattern=".*\\.yourextension" /> <data android:scheme="content" /> </intent-filter> 
0
source share

I have the same problem and in my case both other answers do not work. The closest I came when I combine the answers of benjamin and sabadow, and leave a point in the extension, so like this: (I use my custom extension ".trk")

  <!-- For opening/viewing only trk-files: (works in google drive and "File Manager", not gmail) --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND_MULTIPLE" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*t.*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*t.*t.*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*t.*t.*t.*t.*trk" android:host="*" /> <data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*t.*t.*t.*t.*t.*t.*t.*trk" android:host="*" /> </intent-filter> <!-- For catching attachments in Gmail: (also triggers non-trk-files that aren't associated with some other app, but not non-trk-files that already have one of more associations, strangely) --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND_MULTIPLE" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:mimeType="*/*" android:scheme="content" /> </intent-filter> <!-- For catching share actions from apps: (also triggered by sharing actions for all other file types) --> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/*" /> </intent-filter> 

This is a little longer, but you may not need all the lines, such as the SENDTO and SEND_MULTIPLE actions. I just need it to work whenever it can work. Unfortunately, this also causes some, but not all, other cases.

-one
source share