android scanning all .mp3 files in SD Card

2019-02-20 04:23发布

问题:

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 path for SD Card /mnt/sdcard or /sdcard ?

public class PlayerActivity extends Activity
{

    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.player_activity); 
    }
    public class SongsManager 
    {
        // SDCard Path
        //final String MEDIA_PATH = new String(MediaStore.Audio.Media.getContentUri("external").toString());
        private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        public SongsManager()
        {

        }

        public ArrayList<HashMap<String, String>> getPlayList()
        {
         //   File home = new File(MEDIA_PATH);
            File home = Environment.getExternalStorageDirectory();

            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 FileExtensionFilter implements FilenameFilter 
        {
            public boolean accept(File dir, String name) 
            {
                return (name.endsWith(".mp3") || name.endsWith(".MP3"));
            }
        }

    }
}

Thanks in advance.

回答1:

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


回答2:

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



回答3:

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!



回答4:

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.



回答5:

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



标签: android media