Moosa Baloch Moosa Baloch - 5 months ago 32
Android Question

Getting Folder names instead of .mp3 files {Android}

public ArrayList<HashMap<String, String>> getPlaylist() {
File home=Environment.getExternalStorageDirectory();
if (home.listFiles(new FileExtensionFilter()).length > 0) {
Log.d(Variables.DEBUGTAG, "Filter Files=-->" + home.listFiles().length);
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
Log.d(Variables.DEBUGTAG, "Reading Files");

// Adding each song to SongList
songsList.add(song);

}
}
// return songs list array
Log.d(Variables.DEBUGTAG, "Sending files to system");

return songsList;

}

class FileExtensionFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {

return (filename.endsWith(".mp3") || filename.endsWith(".MP3"));
}
}


While getting playlist it returns the folders or paths not the files from my external storage HELP....!!!
home.listFiles() array contains all the folders in emulated/0/sdcard in it..
right now the if block is false because no folder contains the .mp3 extension if i comment the if block and not using FileExtensionFilter then the for block contains folders only not any file....

Answer

You are only looking for files directly under the root folder of the external storage (Environment.getExternalStorageDirectory()), so it makes sense there are no mp3 files there. You should search the sub-folders of that directory recursively in order to find the mp3 files.

Call home.listFiles() (without a filter), and for each directory, call getPlayList(). For that purpose you have to add a File argument to your getPlayList method. Each recursive call would return an ArrayList containing the details of all the mp3 files its sub-folder tree contains.

For example :

public List<Map<String, String>> getPlaylist() {
    File home=Environment.getExternalStorageDirectory();
    return getPlaylist (home);
}

public List<Map<String, String>> getPlaylist(File root) {
    List<Map<String, String>> songsList = new ArrayList<>();
    if (root.listFiles(new FileExtensionFilter()).length > 0) {
        Log.d(Variables.DEBUGTAG, "Filter Files=-->" + home.listFiles().length);
        for (File file : root.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());
            Log.d(Variables.DEBUGTAG, "Reading Files");

            // Adding each song to SongList
            songsList.add(song);

        }
    }
    for (File file : root.listFiles()) {
        if (file.isDirectory ()) {
            songsList.addAll(getPlaylist(file));
        }
    }

    return songsList;
}

Though it would probably be more efficient to do just one iteration of all the Files in the current directory. For sub-directories you'll make a recursive call and for files you'll check the file name to determine if it's an mp3 whose info should be added to the list.

Comments