pathPattern to match file extension does not work if a period exists elsewhere in the file name?
The android team chose an unfortunate way to implement pathPattern. You can take a look at how the pattern matching is implemented in the android.os.PatternMatch class:
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/PatternMatcher.java
We're used to .* working like it does in a regular expression, where the * match is greedy and will match as many characters as possible. In PatterMatch's implementation, the match is not greedy. The .* will match as many characters as it can, until it finds a match for the next character in the string.
Example:
String: "/mnt/my.file.mytype"
pathPattern: ".*\\.mytype"
The ".*" in the pathPattern will match the substring "/mnt/my", and hence will fail to match the string.
Given this limitation, I don't see a way to write a pathPattern that can match any string that ends in ".mytype". The best you can do is follow Jason's solution to add additional patterns to match paths with as many dots as you are willing to specify patterns.
Android's intent-filter matches files regardless of their extension
Quoting the documentation for android:path*
attributes:
These attributes are meaningful only if the scheme and host attributes are also specified for the filter.
You do not appear to have a scheme.
Open a certain file type in my app
My problem has been solved after I added mimeType attribute:DataMimeType = "*/*"
android:mimeType="*/*
Android intent filter not working
So here is what I ended up with:
<!--Mime type set -->
<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="http" />
<data android:scheme="https" />
<data android:scheme="content" />
<!-- Valid mime types -->
<data android:mimeType="application/vnd.google-earth.kml+xml" />
<data android:mimeType="application/vnd.google-earth.kmz" />
<data android:mimeType="application/gpx+xml" />
<!-- Invalid mime types used by some bad software -->
<data android:mimeType="application/kml" />
<data android:mimeType="application/kmz" />
<data android:mimeType="application/gpx" />
<data android:mimeType="application/kml+xml" />
<data android:mimeType="application/kmz+xml" />
<data android:mimeType="application/vnd.google-earth.kml" />
<data android:mimeType="application/vnd.google-earth.gpx" />
<data android:mimeType="application/vnd.google-earth.kmz+xml" />
<data android:mimeType="application/vnd.google-earth.gpx+xml" />
<data android:mimeType="text/kml" />
<data android:mimeType="text/kmz" />
<data android:mimeType="text/gpx" />
<data android:mimeType="text/kml+xml" />
<data android:mimeType="text/kmz+xml" />
<data android:mimeType="text/gpx+xml" />
<data android:mimeType="text/xml+kml" />
<data android:mimeType="text/xml+kmz" />
<data android:mimeType="text/xml+gpx" />
</intent-filter>
<!-- Mime type not set but valid extensions -->
<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="http" />
<data android:scheme="https" />
<data android:scheme="content" />
<data android:host="*" />
<data android:pathPattern="/.*..*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*\\.kmz" />
<data android:pathPattern="/.*..*\\.gpx" />
<data android:pathPattern="/.*..*\\.kml" />
<data android:pathPattern="/.*..*\\.kmz" />
<data android:pathPattern="/.*\\.gpx" />
<data android:pathPattern="/.*\\.kml" />
<data android:pathPattern="/.*\\.kmz" />
</intent-filter>
<!-- Invalid mime type but valid extensions -->
<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="http" />
<data android:scheme="https" />
<data android:scheme="content" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern="/.*..*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*\\.kmz" />
<data android:pathPattern="/.*..*\\.gpx" />
<data android:pathPattern="/.*..*\\.kml" />
<data android:pathPattern="/.*..*\\.kmz" />
<data android:pathPattern="/.*\\.gpx" />
<data android:pathPattern="/.*\\.kml" />
<data android:pathPattern="/.*\\.kmz" />
</intent-filter>
Seems to be working on all the file manager I tested (FX, ES, Astro, File Commander).
(For those wondering about the weird path patterns see pathPattern to match file extension does not work if a period exists elsewhere in the file name?)
Related Topics
Stopping an Android App from Console
How to Hide a Button Programmatically
Fragments Onresume from Back Stack
Android - Extracting Cookies After Login in Webview
Android Type_Linear_Acceleration Sensor - What Does It Show
How to Retrieve the Logged in Google Account on Android Phones
Android:Get View Reference to a Menu Item
Adding Ripple Effect to Recyclerview Item
Android Character by Character Display Text Animation
Android Vpnservice to Capture Packets Won't Capture Packets
Android:Inapp Purchase Receipt Validation Google Play
How to Center Icon and Text in a Android Button with Width Set to "Fill Parent"
Navigationview and Custom Layout
How to Create a Relativelayout Programmatically with Two Buttons One on Top of the Other
How to Get Android Device Screen Size
Get List of Installed Android Applications
Apache Commons Codec with Android: Could Not Find Method
How to Exclude Certain Messages by Tag Name Using Android Adb Logcat