Android How To Create Intent Filter For Custom File Extension That Does NOT Make It Part Of A Chooser For Everything On The Phone


Answer :

The only way to solve this problem is add 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> 

That is because in documentation says that android:pathPattern only works if has an scheme and host defined. http://developer.android.com/guide/topics/manifest/data-element.html

Hope it helps.


I've been struggling with this quite a bit for a custom file extension, myself. After a lot of searching, I found this web page where the poster discovered that Android's patternMatcher class (which is used for the pathPattern matching in Intent-Filters) has unexpected behavior when your path contains the first character of your match pattern elsewhere in the path (like if you're trying to match "*.xyz", the patternMatcher class stops if there's an "x" earlier in your path). Here's what he found for a workaround, and worked for me, although it is a bit of a hack:

PatternMatcher is used for pathPattern at IntentFilter But, PatternMatcher's algorithm is quite strange to me. Here is algorithm of Android PatternMatcher.

If there is 'next character' of '.*' pattern in the middle of string, PatternMatcher stops loop at that point. (See PatternMatcher.java of Android framework.)

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

Quite strange, isn't it. To workaround this - actually reduce possibility - developer should use annoying stupid pathPattern.

Ex. Goal : Matching uri path which includes '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 issued when matching with custom file extention.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable