I\'m trying to scan all .mp3 files in my SD card and save its name. here is a fragment of code which is responsible for that. 1.What i\'m doing wrong? 1. Which is correct
I'm not sure what's wrong in your code. But you can give this approach a try:
public HashMap scanDirectory(File dir) {
HashMap song;
String mp3Pattern = ".mp3";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(mp3Pattern)){
song = new HashMap();
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 song;
}
To scan entire whole sdcard for mp3 call scanDirectory(Environment.getExternalStorageDirectory());