0

I am trying to filter the files to get only specific files with suffixes .ml, how can I achieve this:

I tried:

    MimeTypeMap map = MimeTypeMap.getSingleton();
    String mimeType = map.getMimeTypeFromExtension(".ml");
    intent.setDataAndType(uri, mimeType);///mimeType = null

Tried this, also fails:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,false);
    String[] mimeTypes = {"application/ml"};
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    intent = Intent.createChooser(intent,"Choose a file...");

It is obvious that these kind of files does not have mime types, is there any approach to filter the files based on suffixes with intent file chooser?

In other words, How to query files with specific extensions in URI object in Android for file chooser intent?

In this page I found a helpful solution to create a recursive approach to get the files and create a list view, but in this way we have got rid of the browsing and choosing the file and this will take time in phones that has a lot of folders,

Is there any other way to still browsing files with file chooser In Android?

Android file chooser with specific file extensions

SOLUTION: Inspired from JASON G PETERSON Solution in the link above, I created the file chooser myself that retrieves the folders and the filtered files and I let the user choose which folder he want to browse and navigate from through onclicklister on items in the listveiw and update a list view.

public ArrayList<File> startBrowse(File dir, String pattern) {
    browseResult = new ArrayList<>();
    browseResultNames = new ArrayList<>();

    File listFile[] = dir.listFiles();
    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {
            ///if it is folder just add it
            if (listFile[i].isDirectory()) {
                    browseResult.add(listFile[i]);
                    browseResultNames.add(listFile[i].getPath());
            }
            else {
                if (listFile[i].getName().endsWith(pattern)){
                    browseResult.add(listFile[i]);
                    browseResultNames.add(listFile[i].getPath());
                }
            }
            }
        }
    return browseResult;
    }
Nadin Martini
  • 193
  • 2
  • 4
  • 13
  • Not everything has file extensions. For example, this Web page's URL does not have a file extension. – CommonsWare Mar 31 '23 at 14:16
  • yes thank you, all the mime types did not work and we can not add the types through intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes) – Nadin Martini Apr 04 '23 at 12:05
  • "I created the file chooser myself that retrieves the folders and the filtered files" -- note that you will not be able to access many files that way, including most of the contents of `Documents/` and `Downloads/` on Android 11+. – CommonsWare Apr 04 '23 at 12:33
  • I tested this on Android 11 emulator and it is fine – Nadin Martini Apr 04 '23 at 13:12
  • For these special file extensions it is working fine, tested on Android 11 and Android 13 – Nadin Martini Apr 04 '23 at 13:27

0 Answers0