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;
}