Intent filter to download attachment from gmail apps on Android
Asked Answered
D

2

15

I have android application with intent filter (ACTION_VIEW) to open file and import it into my application. I wish to download file attachment from gmail app into my application. Some of file type (i.e. jpg, png, txt) are saved correctly, but some are not (i.e doc, xls, ppt). I believe I have the correct intent filter for my activity since it works from other app (i.e. dropbox), but not gmail app. Is there any solution for this ?

Diegodiehard answered 16/11, 2011 at 8:24 Comment(0)
A
8

I was able to make the download and preview buttons pop up on Android in GMail by removing the scheme data filter in my intent (delete the scheme line and give it a try):

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

However, as per the Android documentation, "If a scheme is not specified for the intent filter, all the other URI attributes are ignored." With the scheme and URI attributes removed, the only other way to filter the intents is using Mime type, and we all know that custom file extensions do not have registered mime types.

For reference, URI are of the form:

  • scheme://host:port/path
  • pathPrefix
  • pathPattern

So without a scheme, all of that drops. After discovering the above, I tried the obvious -- use a " * " for the scheme, and even tried " .* ". Neither of those worked. I hope someone else can build off my trials. But I believe it has to do with selecting the correct scheme. Unfortunately, the only schemes I know of are http https content and file, and none of the above are the magic bullet.

EDIT::::::::

I solved this yesterday. Please see my solution:

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/*" host="*" android:pathPattern=".*.ext" android:scheme="content" />
</intent-filter>

This intent will cause gmail to display the Download / Preview buttons. In fact, this will also cause your app to open when .ext files are sent as attachments to the regular email client as well.

Aminta answered 18/1, 2012 at 18:30 Comment(12)
What permission does the app needs to read GMail 'content' URI published by the Preview button? My app has no permissions and when I try to open it I get no-permissions exception. I have no problem reading the 'file' data published by the Download button.Acerb
User -- no idea. I have the following permissions in my app, but I added them in due to different uses, not for the GMail preview / download. Can you also verify that the intent I posted causes gmail download / preview to be displayed? I know I myself have tested it in Froyo, Gingerbread and Ice Cream sandwich and it worked for me in all of them.Aminta
This is causing my intent to filter to trigger anytime I get a new gmail in my notification bar and I go to open it. Is anyone else having that problem? It is definitely this filter causing it.Coarse
As the previous comment mentions, setting the mime-type to application/* means the user will get a chooser asking to open all new email notifications in Gmail/Email and your app. Instead use: <data android:mimeType="application/octet-stream" android:host="*" android:pathPattern=".*\\.extension" />Albertinaalbertine
Thank you richardleggett!!! Your soltuion worked. After weeks of searching and trial and error, yours was finally what did it. You should answer the question on its own so people can see your answer betterCoarse
That's strange...this solution didn't work for me until I removed the host attributeBidet
Thanks richardleggett from me too. You helped me solve it for browers, file explorers, email attachments and not being in the wrong app chooser: #22550627Natalianatalie
<data android:mimeType="application/octet-stream"/> should be enough. The pathPattern does not do anything - at least, not on my Nexus 5. This is because the content uri (dat) does does not include the original path and so cannot match the pathPattern. Also I'm pretty sure the \\ in the pathPattern should be a single backslash. If so, then further evidence the pathPattern is being ignored.Barite
All solutions seem to either open all GMail file attachments or none. pathPattern does not work because as @Barite Carter said, the content URI does not include the original path. Is there no good solution for opening a file extension *.myfile in my app while ignoring pdf, xls, doc, etc??? The content url looks like this 'content://gmail-ls/[email protected]/messages/267/attachments/0.1/BEST/false'Burrton
is there any way to open .jpg attachment into my application?Demicanton
This question is after using Gmail icon in the Chromebook and the web-version of Gmail using Chrome for Chromebooks. Well I tried the Gmail android app in my Chromebook and it worked fine. Worked flawless receiving intent requests and my app could interpret and display the data. Also using the Chromebook standard Filer app the intents worked. Like with a regular Android phone. It looks to be an issue with the web-reader Chrome for Chromebooks and nothing else? The Chrome android app is not able install in a Chromebook (can't see it in Google play) but if it would I guess it would have worked?Slovakia
The issue is about having the app popping up as a possible selection to where Gmail should send the data. The data is not a file but a byte array. The first comment by user xxx here should on how to get the byte array read and go on from the Sfaruq681 answer at #13550059Slovakia
C
0

Since this is one of top question at google related to "gmail attachment intent filter" and I found above answer not working in my case I post the result of my research.

In order to register on intents from gmail, we need to support content scheme:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="content" android:mimeType="*" android:host="*" />
</intent-filter>

In case of attachments that I tested, URI did not contained file extension, even if it was displayed in gmail, so usage of android:pathPattern blocked receiving gmail intents.

Due to the fact, that registering to all mimeTypes is an overkill, I debugged contents of Intent object (on Java side) and found that in my application text/plain is enough (so your homework is to find proper mimeTypes for your application). My final intent-filter looks like that:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="content" android:mimeType="text/plain" android:host="*" />
</intent-filter>
Carlton answered 17/11, 2016 at 22:30 Comment(1)
What about android:scheme="file". Is that not needed?Cobden

© 2022 - 2024 — McMap. All rights reserved.