Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all files with certain extension on Android?

I'm using a fileBrowser to find the files on the phone, but I wanted to show all files that my app can open to the user, and then the user chooses one. Like the Music Player, that show all songs on phone, on the sdcard and in the internal memory, not only the ones in the folder where the user is.

like image 726
Vitor Rangel Avatar asked Aug 12 '11 12:08

Vitor Rangel


People also ask

How do I search for all files with specific extensions?

For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.

How do I see file extensions on Android?

Single Folder Tap the three vertical dots at the top right. Click on the “View Settings” option in the menu. Navigate to the “Advanced” tab. Check (to show) or uncheck (to hide) the “Show file extensions” option.

Which one of the following is the extension of the main activity file in Android Studio?

Which one of the following is the extension of the activity main file in Android studio1) . xml. 2).


2 Answers

Use file name filters while listing out the files. The below sample lists out all mp3 files in a given root directory (Note - The below code doesn't do it recursively 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();
    }
};
like image 194
bluefalcon Avatar answered Sep 28 '22 15:09

bluefalcon


I don't know what FileBrowser implementation you are using but a good one should accept a FileFilter. You can implement your own filter providing code for public abstract boolean accept (File pathname)

like image 28
Vlad Avatar answered Sep 28 '22 13:09

Vlad