1

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

Rohan Pande
  • 301
  • 1
  • 5
  • `.so`? Is it an elf binary? – Darkman Sep 21 '22 at 14:40
  • You can't make custom extensions and expect Android or any other apps to support them. These mimes are hard-coded in the system/app. Other apps might support custom mimes like `x-javascript` or `x-jsonld`. So it is up to the other apps to support them or not. You cannot expect other apps to know how to read/parse/todo with that file. – Darkman Sep 21 '22 at 16:34
  • I am having my own file with this extension and I have my own application I just want that I click on my file my own application should open. Then what is the use of vendor mime types? Can you correct me? @Darkman – Rohan Pande Sep 22 '22 at 00:50
  • The problem is how do you want to click that file? Using a file manager? It will look upon supported apps (apps that know what to do with that file). Unless you don't have to click it, it basically mean you can just call your activity to open it. No need mimes whatsoever. – Darkman Sep 22 '22 at 06:58
  • But suppose I have a pdf document and if I want to open that document then it shows a list of apps through which I can open. In the similar way how to have my app appear in that list for my custom file?@Darkman – Rohan Pande Sep 23 '22 at 01:11
  • 1
    I have the same problem too. Have posted [this](https://stackoverflow.com/questions/71121617/associating-a-custom-file-with-my-android-app) question several months ago, but no response yet. :( – NightFuryLxD Sep 23 '22 at 05:20

1 Answers1

0

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();
    }
}
Darkman
  • 2,941
  • 2
  • 9
  • 14