Search code examples
androidandroid-intentmime-typesintentfilter

Registering an android app to receive certain file types


I would like my application to be able to receive vCards. Typically from an email attachment, but also files,etc. Unfortunately it does not show in the "Open with" / "Share with" menu.

This is the activity definition in the manifest:

     <activity
        android:name=".MyMainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter tools:ignore="AppLinkUrlError">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.BROWSABLE"/>
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
            <data android:scheme="http" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="https" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="content" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="file" android:host="*" android:pathPattern=".*\\.vcf"/>
        </intent-filter>
    </activity>

My test vCard email attachment has a .vcf file extension, and the email marks the mimetype as text/vcard. I've used a *.* mimetype for testing - clearly this would not be appropriate for production code.

The above code is cribbed from other stackoverflow questions, blog posts, etc. I initially started with the following (both with a mimetype of text/vcard and then */*:

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

Solution

  • For Web URLs and most on-device purposes, this should work with "Open With" sorts of options:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/vcard" />
        </intent-filter>
    

    If you wanted to also support "Share With", you could try:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/vcard" />
        </intent-filter>
    

    Note that how you get the content varies between those two actions:

    • For ACTION_VIEW, it is getData() on your Intent
    • For ACTION_SEND, look in the EXTRA_STREAM extra for a Uri or in the EXTRA_TEXT extra for the actual vCard text