Search code examples
androidandroid-manifestintentfilter

Associate a new file with Android app


I would like to register a new file extension (.db_backup) so that all files with this extension open with my app.

Basically files of this type are backed up to email as an attachment. While opening the attachment, I want all files of this type to open directly with my app.

I was successful in doing so by mentioning the android:mimeType = application/*

But now all applications (PDF, APK) open with my app also (which is obvious since * is mentioned). Please tell me what android:mimeType shall I mention in the manifest? My AndroidManifest.xml is given:

<activity
        android:name="com.package.RestoreFromMail"
        android:configChanges="keyboardHidden|orientation"
        android:label="@string/restore_mail" >
        <intent-filter android:priority="1" >
            <category android:name="android.intent.category.DEFAULT" >
            </category>

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

            <data
                android:host="*"
                android:pathPattern=".*\\.db_backup"
                android:scheme="http" >
            </data>
        </intent-filter>
        <intent-filter android:priority="1" >
            <category android:name="android.intent.category.DEFAULT" >
            </category>

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

            <data
                android:host="*"
                android:pathPattern=".*\\.db_backup"
                android:scheme="file" >
            </data>
        </intent-filter>
        <intent-filter android:priority="1" >
            <category android:name="android.intent.category.DEFAULT" >
            </category>

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

            <data
                android:host="*"
                android:mimeType="application/*" >
            </data>
        </intent-filter>
    </activity>

Solution

  • This is how I have the intents setup in my app right now. Just substitute .ext for your extension. Also notice that I am using mimeType="*/*". Had to do this to make it work with Astro file manager. Got it to work with email as well now by removing the android:host="*" in the one with the content scheme.

    <!-- For email -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="content" />
        <data android:pathPattern=".*\\.ext" />
        <data android:mimeType="application/octet-stream" />
     </intent-filter>
    
      <!-- For http -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http" />
        <data android:host="*" />
        <data android:pathPattern=".*\\.ext" />
        <data android:pathPattern=".*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:mimeType="*/*" />
     </intent-filter>
    
    <!-- For https -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="https" />
        <data android:host="*" />
        <data android:pathPattern=".*\\.ext" />
        <data android:pathPattern=".*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:mimeType="*/*" />
     </intent-filter>
    
    <!-- For file browsers and google drive -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file" />
        <data android:host="*" />
        <data android:pathPattern=".*\\.ext" />
        <data android:pathPattern=".*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.ext"/>
        <data android:mimeType="*/*" />
     </intent-filter>