Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by mimetype or extention in native Android file picker

In Android, one can use the ACTION_OPEN_DOCUMENT Intent to open the native file picker and select for example an .mp4 file. This is achived by setting the mime type to video/mp4 using the following code:

public static void pickFile(Context mContext, int REQUEST_CODE) {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("video/mp4");  
        ((Activity) mContext).startActivityForResult(intent, REQUEST_CODE);
    }

In my project, I want to pick a custom extension file whose mime type is not known in android's MimeTypeMap like for example .qgs or .dcm files.

To solve this I see two possibilities that we, so far, failed to implement:

  1. filter by extension in the ACTION_OPEN_DOCUMENT Intent
  2. register a new mime type to android so that it can be used with the ACTION_OPEN_DOCUMENT Intent

is either of those options doable and how? or are there other approaches I missed without coding my own file picker?

like image 578
Levey Avatar asked Dec 15 '15 07:12

Levey


People also ask

How to find MIME type of a file in Android?

Retrieve a file's MIME type To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.

How do you get the Mimetype of a file in Kotlin?

In Kotlin, val extension: String = file. name. split("."). last() eliminates the need for getExtension and the more than one .


2 Answers

You can try creating a custom file picker and pass the mount storage points and recursively iterate on all the file types:

public void scanFiles(File file) {
File[] fileArray = file.listFiles(); 
for (File f : fileArray){
    if (f.isDirectory()) 
      scanFiles(f);
    if (f.isFile() && (f.getpath().endswith("..qgs") || //any extensions)) {
        //Add to your list 
       }
    }
}

And call scan files for all the mount points in android using a process builder and mount command and you can add checks for all the mount points you wish to support.

like image 95
shubhamgarg1 Avatar answered Sep 20 '22 19:09

shubhamgarg1


one can use the ACTION_OPEN_DOCUMENT Intent to open the native file picker

That is not a "file picker". There is no requirement that every DocumentsProvider on the device be serving documents that happen to be files.

I want to pick a custom extension file whose mime type is not known in android's MimeTypeMap like for example .qgs or .dcm files

There is no requirement that every DocumentsProvider on the device be using documents that have filenames for their files. The names that you see are "display names", and while for some providers they will be filenames, for other providers they can be anything that the provider wants.

is either of those options doable

No.

or are there other approaches I missed without coding my own file picker?

If you only want files, there are plenty of existing file picker implementations available in libraries. ACTION_OPEN_DOCUMENT is there for when you want to work with all possible document sources (removable storage, cloud providers, etc.), not just files.

like image 31
CommonsWare Avatar answered Sep 16 '22 19:09

CommonsWare