Yasin Arefin
Yasin Arefin

Reputation: 55

Associate file type with app (Android)

What I want to do is make my app default to open files like .mp3 or .pk. I want to process the file then show the result . Possible ways for doing that ?

Upvotes: 0

Views: 434

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007564

First, you cannot make your app be the default for any particular type of content. If there are other apps that support that type of content, the user will get to choose what app to use. It is the user's device, not yours.

Second, file extensions are not widely used on Android, and their use will steadily decline. MIME types are more reliable.

To be an option for opening files of a particular MIME type, implement an activity, where the <activity> element in the manifest has an <intent-filter> for ACTION_VIEW, your desired MIME type (or MIME type wildcard pattern), and categories and schemes that fit your use case. For example:

    <activity android:name="com.android.gallery3d.app.MovieActivity"
            android:label="@string/movie_view_label"
            android:configChanges="orientation|keyboardHidden|screenSize">
        <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="rtsp" />
         </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" />
            <data android:scheme="https" />
            <data android:scheme="content" />
            <data android:scheme="file" />
            <data android:mimeType="video/mpeg4" />
            <data android:mimeType="video/mp4" />
            <data android:mimeType="video/3gp" />
            <data android:mimeType="video/3gpp" />
            <data android:mimeType="video/3gpp2" />
            <data android:mimeType="video/webm" />
            <data android:mimeType="video/avi" />
            <data android:mimeType="application/sdp" />
         </intent-filter>
         <intent-filter>
            <!-- HTTP live support -->
            <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" />
            <data android:scheme="https" />
            <data android:mimeType="audio/x-mpegurl" />
            <data android:mimeType="audio/mpegurl" />
            <data android:mimeType="application/vnd.apple.mpegurl" />
            <data android:mimeType="application/x-mpegurl" />
         </intent-filter>
    </activity>

(from this AOSP app)

Upvotes: 2

Related Questions