How to find all files with certain extension on Android?
Asked Answered
Z

2

9

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.

Zeringue answered 12/8, 2011 at 12:39 Comment(0)
R
9

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();
    }
};
Registered answered 12/8, 2011 at 13:0 Comment(2)
Thanks very much. Now I'm trying to make this to get all the files in the sub-folders, and it's only a matter of time.Zeringue
I think the string check should better be : name.length()>=4&&name.subString(name.length-4).toLowerCase().equals(".mp3") , or something like that (didn't check). The reason is that you won't need to go over the string more than once this way.Elongation
I
1

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)

Intarsia answered 12/8, 2011 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.