Search code examples
androidintentfilter

How do I register my app in Android to open email attachments with a custom filetype?


My app is supposed to handle my custom .rmc files. My users are going to receive them via email and I want my app to be able to open them but ideally open as few other filetypes that my app can't handle as possible.

I use the following intent-filter in Android with unfortunately doesn't work for opening attachemnts in gmail (gmail gives an error message that there's no app to open rmc files).

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <action android:name="android.intent.action.VIEW"/>


            <category android:name="android.intent.category.LAUNCHER"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="content" android:mimeType="*/*" android:host="*" android:pathPattern=".*\\.rmc"/>
            <data android:mimeType="application/octet" />
            <data android:mimeType="application/octet-stream" />
        </intent-filter>

For comparison the following filter catches the file but catches way too much:

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <action android:name="android.intent.action.VIEW"/>


            <category android:name="android.intent.category.LAUNCHER"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/*" />
        </intent-filter>

Solution

  • My app is supposed to handle my custom .rmc files.

    There is very little support for file extensions in Android.

    I use the following intent-filter in Android with unfortunately doesn't work for opening attachemnts in gmail

    There is no requirement for Gmail, or any other app, to use content Uri values that use a file extension. So, you're welcome to have that <intent-filter>, but do not assume that it will work with all email clients or any other arbitrary app.

    How do I register my app in Android to open email attachments with a custom filetype?

    You don't.

    If the email clients sending the emails will supply a specific distinctive MIME type for your content, use that MIME type for android:mimeType. For example, if you will be sending the emails from some server, you should be able to control how the attachment is added and what MIME type it uses.

    Otherwise, find some distribution mechanism for this data that uses a distinctive MIME type (e.g., download from a Web server, where you configure the server to serve your files with that MIME type). Then, use that MIME type for android:mimeType.