I want to be able to download a file with a particular extension from the \'net, and have it passed to my application to deal with it, but I haven\'t been able to figure out
On Android 4 the rules became more strict then they used to be. Use:
<data
android:host=""
android:mimeType="*/*"
android:pathPattern=".*\\.ext"
android:scheme="file"
></data>
I must admit that the simple task of opening attachments from emails and files from the filesystem on Android has been one of the more maddening experiences ever. It is easy to handle too many files or too few. But getting it just right is hard. Most of the solutions posted on stackoverflow didn't work correctly for me.
My requirements were:
Probably the best way to go about this task is to specify a custom MIME Type for your attachments. And you will probably also choose to have a custom file extension. So let's say that our app is called "Cool App" and we generate file attachments that have ".cool" at the end.
This is the closest I got got to my goal and it works... satisfactory.
<!-- Register to handle email attachments -->
<!-- WARNING: Do NOT use android:host="*" for these as they will not work properly -->
<intent-filter>
<!-- needed for properly formatted email messages -->
<data
android:scheme="content"
android:mimeType="application/vnd.coolapp"
android:pathPattern=".*\\.cool" />
<!-- needed for mangled email messages -->
<data
android:scheme="content"
android:mimeType="application/coolapp"
android:pathPattern=".*\\.cool" />
<!-- needed for mangled email messages -->
<data
android:scheme="content"
android:mimeType="application/octet-stream"
android:pathPattern=".*\\.cool" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- Register to handle file opening -->
<intent-filter>
<data android:scheme="file"
android:mimeType="*/*"
android:pathPattern=".*\\.cool"
android:host="*"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Notes:
pathPattern
seems to be more or less ignored for attachments (when using android:scheme="content"
). If somebody gets the pathPattern to respond only to certain patterns I would be thrilled to see how.android:host="*"
attribute.intent-filter
blocks are merged but I haven't verified this.android:scheme="http"
can be used. Note that certain browsers might mess up the android:mimeType
so experiment with android:mimeType="*/*"
and check in the debugger what is actually passed through and then tighten the filtering to not end up being that annoying app that handles everything.intent-filter
was tested with the Samsung's "My Files" app on a Galaxy S3. The FX Explorer still refuses to properly open the file and I also noticed that the app icon is not used for the files. Again, if anyone gets that to work please comment below.I hope you will find this useful and that you won't have to waste days going through all possible combinations. There is room for improvement so comments are welcome.
Rather than android:path
, try android:mimeType, with a value of the MIME type of this particular piece of content. Also, android:path
does not accept wildcards -- use android:pathPattern for that.
Read this https://developer.android.com/training/sharing/receive , this could help to get started. then in the manifest file try the below intent filter when registering receiver in the activity to be opened
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="content"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/your_extension" />
</intent-filter>
make sure that the extension is provided without "." in it
If you want the files to be opened directly from Gmail, dropbox or any of the buildin android file tools, then use the following code (delete 'android:host="*"' that made the file unreachable for gmail) :
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="content" android:pathPattern=".*\\.kdb"
android:mimeType="application/octet-stream"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="file" android:mimeType="*/*"
android:pathPattern=".*\\.kdb"/>
</intent-filter>
The data filter must be written in one statement as per Android version 4.x
Brian's answer above got me 90% of the way there. To finish it off, for mime type I used
android:mimeType="*/*"
I suspect that previous posters have attempted to post the same detail, but the withough qoting the star slash star as code, stackoverflow diplays it as just a slash.