4

I want to associate files of particular extension (say, any file with the extension .xyz) with my app. This means, when user taps SaveFile1.xyz in File explorer (or other places, like gmail, drive etc.), my app should launch and the Uri of the file will be passed in an intent to my activity.

While this is the expected behavior, I don't know how to associate custom files. I have read and understood how intent matching works and the possible values in data tag, but it didn't work. I have checked some stackoverflow answers (Ex, this and this), but they use file scheme and pathPatterns.

We cannot use the pathPattern because we get a content uri (Uri with scheme=content), which is the abstracted path and will not have any extensions.

The mime type setting can be used only for the predefined values (like image/png can associate all png images with the app).

I could set the mime type as "*/*", but I don't want my app to accept every file (and then reject it in onCreate or something).

Has anyone solved this problem? Any help will be greatly appreciated.

I have the following in the activity tag of the manifest file.

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <!--<data android:mimeType="application/pdf"/>
                <data android:mimeType="image/png"/>-->
                <!--<data android:scheme="content" android:mimeType="*/*"/>-->
            </intent-filter>
        </activity>

Edit1: I set the mime type as "*/*" and added the following line in Activity.onCreate to see the type in the intent (and set it in the manifest file) ...

Intent dataIntent = getIntent();

Log.i(TAG, "dataIntent.getType() = " + dataIntent.getType());

.... but got the following as output.

dataIntent.getType() = 

Its not even null.

NightFuryLxD
  • 847
  • 5
  • 15

0 Answers0