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
The problem is with what android consider as external storage. It is not always the sd card that you add to your device. the internal memory can also be external storage. /sdcard or /mnt/sdcard is the same location and it may point to your internal storage. so you have to scan the /mnt folder completely. like in some device /mnt/sdcard will be internal memory and /mnt/extSdCard will be the one you have added. File home = Environment.getExternalStorageDirectory(); this method will return the first external storage and not all.
I'm not sure what's wrong in your code. But you can give this approach a try:
public HashMap<String, String> scanDirectory(File dir) {
HashMap<String, String> 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<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 song;
}
To scan entire whole sdcard for mp3 call scanDirectory(Environment.getExternalStorageDirectory());
The Environment.getExternalStorageDirectory().getPath()
is usually of the form
"/storage/emulated/0/" or so.
You split this to get the root -
String media_path = Environment.getExternalStorageDirectory().getPath();
String[] splitPath= media_path .split("/");
final String MEDIA_PATH = "/" + splitPath[1] + "/";
This works!
try this code snippet
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
try this: final String MEDIA_PATH = "/storage/";