How to find all files with a specific extension on Android?

I use fileBrowser to search for files on the phone, but I wanted to show all the files that my application can open to the user, and then the user selects them. Like the Music player, which shows all the songs on the phone, on the SD card and in the internal memory, not only those that are in the folder where the user is located.

+7
source share
2 answers

Use file name filters when listing files. The example below lists all mp3 files in the given root directory (Note. The code below is not recursive for all folders under root ) -

 String files[] = root.list(audioFilter); FilenameFilter audioFilter = new FilenameFilter() { File f; public boolean accept(File dir, String name) { if(name.endsWith(".mp3") || name.endsWith(".MP3")) { return true; } f = new File(dir.getAbsolutePath()+"/"+name); return f.isDirectory(); } }; 
+8
source

I donโ€™t know which FileBrowser implementation you are using, but a good one should accept FileFilter . You can implement your own code for the filter public abstract boolean accept (File pathname)

+1
source

All Articles