In Android, for a custom file extension (for ex: file1.extension) what should be the Mime Type for it?

Viewed 46

For an Android Application, I am having a custom file with (.xyz) extension but in the AndroidManifest.xml what should be the value for the below code in the intent filter?

<data android:mimeType= "___________"/>

The thing which I want is that I have a file with that extension in the phone so when I click on that file my application should open or my application should show in the OpenWith Dialog box.

"*/*"

If I put the above value in the code then it prompts for every file type.

I have tried this vendor mime types by creating a public final class and declared all the values below it but that doesn't seem to be working for me.

Reference link for vendor mime types which I looked for: https://android.googlesource.com/platform/development/+/05523fb0b48280a5364908b00768ec71edb847a2/samples/NotePad?autodive=0

Can anyone help me in this? Or any sources so that I can achieve this?

Thanks in advance

1 Answers

If you want a file manager to include your app when you're clicking a file with a custom extension, adding this in the Manifest.xml file might work:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="*" />
    <data android:scheme="content" />
    <data android:scheme="file" />
    <data android:mimeType="text/*" />
    <data android:mimeType="application/octet-stream" />
    <data android:pathPattern=".*\\.customextension" />
    <data android:pathPattern=".*\\.pdf" />
</intent-filter>

To get the extension, you can just sanitise the string like so:

private final String getFileExtension(final String dataString)
{
    //content://com.myapp.files/data/data/com.myapp/files/home/bsdiff/trunk/README.md (NO-NO)
    //content://com.myapp.files/storage/emulated/0/README.md (NO-NO)
    //file:///storage/emulated/0/README.md (YES)

    final String ext = dataString.replaceFirst("^.*\\/", "").replaceFirst("^.+\\.", "");

    if(ext.isEmpty() /*file.*/ || ext.charAt(0) == '.') {
        return null; //No extension.
    } else {
        return ext.toLowerCase();
    }
}
Related