I need to define IntentFilter
for single file named myfile.ext
. At the moment my manifest looks like:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="file"
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\myfile\\.ext"
/>
</intent-filter>
I have also tried other variants like: android:pathPattern=".*\\myfile.ext"
and so on - but still it doesn't handle my file.
Any clues?
Android Pattern specifics :
.
matches any character.*
matches 0 or more occurrences of the character preceding it..*
matches 0 or more occurrences of any character.\
is used to escape special characters in pattern.\
is also used as an escape character when the string is read from the XML file. Hence to escape special character in a pattern, double-slash\\
has to be used.Issue: In this pattern
".*\\myfile\\.ext"
, you are trying to escape characterm
which is a normal character. Hence it does not make any difference. It is equivalent to".*myfile\\.ext"
. The data uri part of intent isfile:///mnt/sdcard/tmp/myfile.ext
. The pattern is matched against/mnt/sdcard/tmp/myfile.ext
, but it fails..*
tries to match any character until the first occurrence ofm
, which happens to be the 2nd char i.e./mnt
. Pattern expects the next char to bey
, but it getsn
and hence the pattern match fails.Solution: For above path, the pattern
/.*/.*/.*/myfile\\.ext
works.For
/mnt/sdcard/myfile.ext
path, pattern/.*/.*/myfile\\.ext
works. If you are not sure the sub-directory level, you will have to add multiple<data>
element with differentpathPattern
values.Here is the PatternMatcher.matchPattern method used for pattern matching: