Search code examples
androidmime-typesemail-attachmentsintentfilter

how to open an attachment with my application from within the email app? (mime type, intent filter ...)


this may look as a duplicate, but I think I have tried most of the solutions posted with no success.

I am writing an application that at some point sends files by email with a custom extension (.xyz)

I then need to open this files from the email app, and I cannot make it work.

After reading a lot of posts in this site and others, I came up with an intent filter that allows me to open this files from a File Explorer ( but not from the email app).

<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" android:host="*" 
                    android:pathPattern=".*\\.xyz"/> 
 </intent-filter> 

I then tried to open the email and the attachment from within a web browser, and it didn't work. (the file got downloaded, but then I couldn't open it). I got this error in the LogCat

    09-14 12:30:50.516: D/DownloadManager(2341): android.content.ActivityNotFoundException:
    No Activity found to handle Intent 
{ act=android.intent.action.VIEW 
dat=file:///mnt/sdcard/download/samplefile.xyz 
typ=message/rfc822 flg=0x10000000 }

Though I find it strange to use the rfc822 mime type for the file, I changed the intent filter and added

android:mimeType="message/rfc822"

to the data section.

Then I could open the file, just after being downloaded from the browser, but I can no longer open the file from a File Explorer (which makes kind of sense).

Even adding android:mimeType="/" has the same effect. (The mime type of the file seen from within ASTRO File Explorer is empty).

But what I really need is to open the file from the email application, and none of these approaches helped there. I cannot even click on the attachment when opening the email.

I may need to create a custom mime type, but I have not been able to find how to do it.

So, the question is, what do I need to do to open the email attachments (from the builtin email app)with my application?

  • create a mime type? (if so, how?)
  • create a content provider?
  • both? (again, how?)

Solution

  • Just in case, someone has the same issue I had, I was able to finally make it work. The problem I had was that when sending the email with the attachment I was using the mime type "message/rfc822". Changing it to "image/xyz" did the trick, and of course using the intent filter with mime type set to "*/*".

    There's a slgiht different behaviour when sending the email and is that you get a chooser with every app that can SEND, not only mail apps (MMS, WhatsApp, Bluetooth,...) , but in my case it is fine.