Gmail attachment and custom extension - android

I work currently on an Android application that read file with a custom extension.
One of the mandatory features, is that the app must be proposed by gmail when the user receive a mail with the attachment .ourextension.
I did some research, and found that gmail client on Android doesn't rely on the extension, because in the data of the launched intent the file proposed has no extension. It only rely on the mime-type given by the mail client.
The problem is that our custom file are not detected the same way between mail clients. For example, if I send to myself with the gmail webpage our custom file, the mime-type is detect as application/octet-stream. If a friend of mine send with apple mail desktop software, it is detected as a text/xml (which would be nice). And on another mail client, Evolution, the mime-type is text/plain...
Our application can't handle all those types ! Otherwise, it would be proposed for every type of attachment...
Is there any solution for this ?

Solution to open file with custom extension tested with gmail < 4.2, gmail 4.2, Google Drive and file browser
Code to send file :
sendIntent.setType("application/calc1");
Intent-filter :
<!-- Filter to open file with gmail version < 4.2 -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/calc1" />
<data android:pathPattern=".*\\.calc1" />
<data android:host="*" />
</intent-filter>
<!-- Filter to open with file browser or google drive -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.calc1" />
<data android:host="*" />
</intent-filter>
<!-- Filter to open file with gmail version 4.2 -->
<!-- Save file first with "save" button otherwise gmail crashes -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\.calc1" />
<data android:host="*" />
</intent-filter>

It is possible to get this to work from gmail although it's taking me days of effort to figure out all the issues.
The Important Intent Data
act=android.intent.action.VIEW
dat=file:///mnt/sdcard/Download/Availability Focus.inform
typ=application/octet-stream
flg=0x80001 cmp=air.com.extension/.AppEntry
The Intent Filter that was able to capture it (some notes below)
<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="file" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/>
<data android:scheme="content" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/>
</intent-filter>
BOTH the content and file scheme's are required. Without the CONTENT scheme gmail will ignore your filter all together. This seems like a bug. Without the FILE scheme gmail will throw an error saying it doesn't know which application to launch from the intent.
07-19 15:38:19.160: ERROR/Gmail(2220): Coun't find Activity for intent
With both scheme's in place the application receives the intent for both Downloaded and Previewed files. They have to be handled differently.

You can still match extension for gmail
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/*" host="*" android:pathPattern=".*\\.mht" android:scheme="content" />
</intent-filter>
Make the change for mime type to be / and itll only match on extension

Edit: This answer is 3 years old. I didn't have the time to test the other's answers but mine is clearly outdated so please looks for the others answers of this topic !
I didn't found any solutions, the only way to work is with mime-type, so you must pray for your attachment to be recognize with a known mime-type, and handle this. It means that you will interfer with other application, if your file is recognize as html for example.
You need to handle the case that a file is not readable by your application and show a popup for notifying the user that you can't read the file.
Maybe in a year or two, Gmail will provide a good api...
By the way, the last gmail version add a download button that you must handle to avoid crashes, it works by creating a new file with the uri.

Related

Deep Links and Instant Apps

I am attempting to implement deep links within my Instant App, and keep running into issues that seem to be coming from something to do with the base application. Currently, the error I'm receiving from the Play Console states:
You should have at least one active installed app APK that mapped to
site 'example.site.com' through a web intent-filter.
I do have an intent-filters set up in both the instant app and in the full base app. The intent-filters in the base application look like this:
<intent-filter>
<data
android:scheme="http"
android:host="example.site.com"
android:pathPattern="/test/app/" />
</intent-filter>
<intent-filter>
<data
android:scheme="https"
android:host="example.site.com"
android:pathPattern="/test/app/" />
</intent-filter>
And the intent-filters in the instant app module's manifest look like this:
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.site.com"
android:pathPattern="/test/app/" />
<data
android:scheme="http"
android:host="example.site.com"
android:pathPattern="/test/app/" />
</intent-filter>
With the instant app manifest also containing a default url, set up within the activity tag.
I'm not sure what I am missing here. I have set up the intent filters as I've seen elsewhere on the web, in Google's documentation, and in other forum posts where I've seen people encountering issues with this process. I've also hosted the assetlinks.json file in the .well-known directory on my domain, and verified that it is correct and accessible using Google's verification API. Does anyone know what the issue causing this error may be? Thanks!
From what I have read in the documentation you need to make sure that you use an intent-filter with autoVerify, action and categories for the app as well. In your example you can simply copy & paste the intent-filter that you are using in the instant-app to your full app.
In case you are using string resources for scheme, host or pathPattern I would try to avoid this and directly enter your values in the manifest instead.
e.g. (crafted from your post)
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.site.com"
android:pathPattern="/test/app/" />
<data
android:scheme="http"
android:host="example.site.com"
android:pathPattern="/test/app/" />
</intent-filter>

Android: Associate app with custom file type

I've got a file type, the extension is '.rfts' (really it's just storing a JSON string that represents user configurations for an audio amplifier). I'd like to be able to open this file when it's an attachment from an e-mail (Gmail for example) so I can import user settings from another tablet.
Here's what my manifest looks like (note that I didn't include the other activities in this, but there's 4 others that don't have intent filters).
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/RFtheme" >
<activity
android:name=".activity.MainActivity"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.OPENABLE"/>
<data android:scheme="file"/>
<data android:mimeType="*/*"/>
<data android:pathPattern="\\.rfts$"/>
<data android:host="*"/>
</intent-filter>
</activity>
<provider
android:name=".model.FileProvider"
android:authorities="com.rockfordcorp.app3sixty.provider"
android:enabled="true"
android:exported="true" >
</provider>
</application>
I've been trying several other suggested fixes from other questions, but they were for things like opening a pdf from a browser.
When I try to open a .rfts attachment in Gmail, it tells me "You don't have an app that can open this file (.rfts). Try searching google play for one that can"
I'm at a loss as to what I need to be doing here. I don't have any idea what mime Gmail would be using to open a .rfts, nor what scheme it would be using. I've tried a few different combinations but nothings really worked. I just haven't put in the magic combination of category, mimetype, pattern and scheme that Android is looking for to associate this file to my app.
EDIT some success, but not quite there yet.
The questions suggested as fixes are off the mark, and the reason is because the scheme required is actually 'content', not 'file'
The intent filter that works is
<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:mimeType="*/*" android:pathPattern=".*\\.rfts"/>
<data android:scheme="content" android:pathPattern=".*\\.rfts" android:mimeType="application/octet-stream" />
<!-- <data android:host="*"/> -->
</intent-filter>
Without the android:sceheme="content" it does not work.
However, a new problem arises. Gmail now opens all file types that are not previously associated with another app. For example, if I were to try to open a .rfff file, it uses my app. If you try to open a .txt it opens a chooser for Chrome or HTML viewer.
This is close, but it opening other file types is problematic. Android:pathPattern evidently has no effect on what filetype my app is associated with.
As this question was marked as a possible duplicate I want to point out the solution suggested is not working for opening a file from g-mail not web, nor does it cover opening a custom filetype. Using that 'solution' with the file type swapped out causes g-mail to continue to insist there is no app on the device capable of opening the file type.
A different solution likely needs to be provided to associate opening this custom filetype via the intent from Gmail.
Update 2020
Android has moved towards content URIs and MIME-Types for intent filters.
The Problem
A content URI does not necessarily have to contain the file's extension or name and it will be different between different applications that are providing the content/file.
Here are some example content URIs from different email applications for the same email attachment:
Gmail -> content://com.google.android.gm.sapi/some_email#gmail.com/message_attachment_external/%23thread-a%3Ar332738858767305663/%23msg-a%3Ar-5439466788231005876/0.1?account_type=com.google&mimeType=application%2Foctet-stream&rendition=1
Outlook -> content://com.microsoft.office.outlook.fileprovider/outlookfile/data/data/com.microsoft.office.outlook/cache/file-download/file--2146063402/filename.customextention
Samsung Email App -> content://com.samsung.android.email.attachmentprovider/1/1/RAW
As can see they are all different and are not guaranteed to contain anything related to your actual file. Thus, you cannot use the android:pathPattern like most have suggested.
A work around solution for email attachments
<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"/>
<data android:host="*"/>
<!-- Required for Gmail and Samsung Email App -->
<data android:mimeType="application/octet-stream"/>
<!-- Required for Outlook -->
<data android:mimeType="application/my-custom-extension"/>
</intent-filter>
Through testing I found the MIME-Types that Gmail, Outlook, and Samsung Email used and added those to my intent-filter.
Caveats/Gotchas
I found that with my above solution, if I opened any file that was a binary type, it would automatically launch my app. I handled this in my activity by displaying a failed state if we could not parse the file. I figured this was a pretty rare event so it would acceptable.
I could not find any way to launch my app via the file browser without adding <data android:mimeType="*/*"/> to my intent-filter. I couldn't use this because it would then launch my app whenever the user clicked any file on their phone (not just the custom-file-extension ones). I would not recommend adding this to your intent-filter.
I had no luck using android:scheme="file" at all.
I tested this on a Samsung Galaxy S10 on Android 10
Final Thoughts
There is currently no elegant solution for associating your app with a specific extension type in Android. This was the best that I could do in my situation.

Launching custom Android application from Android browser / Chrome

First of all, my question is extremely similar to this, this and this. The Android documentation for what I'm trying to achieve is here. I couldn't get this to work using these resources so please don't mark this as duplicate as it is not.
I have a website and an Android application. The user will be able to scan QR codes that contain links like http://mywebsite.com/map/. When the user tries to open this link, I want Android to show him a chooser dialog where he can choose to open that link using my application. If my application is not installed, it should proceed to the specified website.
I know Chrome allows this by opening the chooser dialog when the user navigates to that address. For example, try downloading the Stack Exchange app and going to this question in Chrome. It will show this:
I have added the following code in AndroidManifest.xml after following the suggestion in the above-mentioned answers:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="mywebsite.com"
android:path="/map"
android:scheme="http" />
<data
android:host="mywebsite.com"
android:path="/animals"
android:scheme="http" />
<data
android:host="mywebsite.com"
android:path="/articles"
android:scheme="http" />
</intent-filter>
Also, I have tried adding android:mimeType="text/plain" to data but it didn't help.
The problem is that when I go to http://mywebsite.com/map or http://mywebsite.com/map/ Chrome just opens the webpage without showing the chooser dialog.
I would like to mention:
following the Android documentation, I have added this code inside one of the activity structures in AndroidManifest.xml. As I am not sure this is the perfect place to add it, I have also tried adding it outside the application structure and directly inside the application structure but it didn't work
this is the only code I have implemented for this to work. If something else is needed please let me know. From what I understand, adding a href to the webpage is only needed when using custom schemas
I do not want to use a custom schema in order to achieve this
I am developing on a Nexus 4, running Android 4.4.2 (latest)
You need to set it up like this :
<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:host="example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
<data
android:host="www.example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
</intent-filter>
Notice that in your case, you would need to use android:pathPrefix instead of android:path.
Just to be sure, you should reset the preferences for your app in case you have accidentally set it to always open the link in chrome rather than show the chooser dialog. Once "Always" is used to open the matching uri, it will never show the chooser.
Second, you can have as many data elements in the intent filter as your want, but it is not necessary to repeat information. You can do the same thing like this:
<data android:host="mywebsite.com"/>
<data android:scheme="http"/>
<data android:path="/map"/>
<data android:path="/animals"/>
<data android:path="/articles"/>
But note that for the path, you can just use a wildcard
<data android:path="/.*"/>
Consider adding an additional
<data android:host="www.mywebsite.com"/>
And finally you may not want to show the chooser dialog but open a specific app intent/activity directly. On your website, if you detect the android user agent, you can create a link url this way:
<a href="intent://mywebsite.com/articles#Intent;package=com.myapp;scheme=http;end;"/>
See here for more details How do I open any app from my web browser (Chrome) in Android? What do I have to do with the A Href link?
Note that with this method if the app is not installed the user will be taken to the Google Play store for the specified app package.
If you are still having problems, check your intent filter priority. http://developer.android.com/guide/topics/manifest/intent-filter-element.html
In my case site URL is: http://www.example.org/mobile/
so putting these code into AndroidManifest.xml inside activity
<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:host="www.example.org"
android:pathPrefix="/mobile/"
android:scheme="http" />
</intent-filter>
Here,
scheme -> Protocol of particular site
host-> Exact site url with WWW
pathprefix - > Your site's sub path if available
Now,
You can search with chrome / etc android browser'search box like example then open chosen dialog ..!!

Android intent filter

I have written the below intent filter to open a text file using my app. It seems to work but only sometimes. For example, if I email a text file, if I choose open from mail, my app is not shown. If I choose save first, then open, my app will be shown. Similar experience with drop box, if I try to open from drop box, my app won't be listed as being able to open but if I export from drop box to sd and use a file manager to open it, my app is listed and works.
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<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="http" android:host="*" android:pathPattern=".*\\.txt" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.txt" />
<!-- <data android:scheme="content" android:host="*" android:pathPattern=".*\\.txt" /> -->
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.txt" />
</intent-filter>
Dropbox and the Email app probably use content providers and don't match the pathPattern. Typically content providers don't include a file extension, but would use the mime type to indicate what type of file is being opened. If you are intending to open any text/plain file, and not necessarily only those which have the .txt extension, then you'd be better off leaving the pathPattern off altogether.
<intent-filter>
<data android:mimeType="text/plain" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Probably beacuse the email app and dropbox uses a different scheme instead of file, http, or https. Try using the mime type attribute only.
EDIT: Acording to the dropbox forums:
FYI, the easiest way to do this now is to open up your app from Dropbox by registering for VIEW with the correct mime-type. Then everything is handed off properly, and saving & watching file changes to re-upload is handled well.
You only have to specify the mime type, and nothing else. I haven't tried this though, but probably this works for the email app as well, as long as the mime types match.
Here is the link if you are interested:
https://forums.dropbox.com/topic.php?id=26035#post-162963

Intent filter to download attachment from gmail apps on Android

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 ?
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.
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>

Categories

Resources