I'm trying to make an mp3 player app, and when I run it from my phone it only reads MP3's that are present on the SD card itself. It does not read any MP3's from the subfolders that are present in the card. I want it to display all the MP3's present in the SD card(including the subfolders).
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
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());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
} }
There is the MusicRetriever example in the Android SDK. It uses a ContentResolver.
Then
WalkDir method
Even better use enhanced for loop as suggested by blackbelt @
Delete only .jpg files from folder in android
Instead of deleting add the path to the hahsmap