Gmail attachment and custom extension
Asked Answered
P

4

7

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 ?

Paxon answered 10/6, 2011 at 8:52 Comment(0)
T
3

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>
Tenenbaum answered 18/1, 2013 at 9:7 Comment(0)
S
2

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.

Sherrellsherrer answered 19/7, 2012 at 18:54 Comment(3)
I just tested this, and it works for Gmail 4.1.2 running on Android 4.0.3. But when I open the same email with Gmail 2.3.6 running on Android 2.3.6, I don't even have the option to open the attachment.Zuber
Tested with Gmail 4.2.1 running on Android 4.2. Works well. Thanks.Simmons
When testing this, make sure you also try to open an attachment with a different extension, that doesn't already have an association. You will find that your intent-filter also matches it. The reason, I think, is that you don't have a host attribute, and without that the pathPattern is ignored (developer.android.com/guide/topics/manifest/data-element.html). If you add android:host="*" then your intent-filter will suddenly not match anymore. The problem seems to be that content:// URIs don't contain the filename (developer.android.com/reference/android/content/ContentUris.html).Ancel
P
0

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.

Paxon answered 10/8, 2011 at 8:47 Comment(0)
V
0

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

Vannessavanni answered 19/1, 2012 at 21:25 Comment(3)
Sorry, I didn't see your answer before, I will try asap if it works, and edit my answer if it does .Paxon
Sorry, but it doesn't work, Gmail only use mime type for the intent.Paxon
Works for me. I have a download and preview button. Tested on a stock Galaxy S phone and a Galaxy S II tablet.Vannessavanni

© 2022 - 2024 — McMap. All rights reserved.