android scanning all .mp3 files in SD Card

◇◆丶佛笑我妖孽 提交于 2019-12-01 12:42:43

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);
    }
}

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!

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.

try this: final String MEDIA_PATH = "/storage/";

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!