1

我正在尝试扫描 SD 卡中的所有 .mp3 文件并保存其名称。这是负责此的代码片段。1.我做错了什么?1. SD 卡 /mnt/sdcard 或 /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"));
            }
        }

    }
}

提前致谢。

4

5 回答 5

5

试试这个代码片段

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);
    }
}
于 2013-08-16T07:28:01.300 回答
0

我不确定您的代码有什么问题。但是您可以尝试这种方法:

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

扫描整个 sdcard 以进行 mp3 调用scanDirectory(Environment.getExternalStorageDirectory());

于 2013-04-18T13:54:24.643 回答
0

通常Environment.getExternalStorageDirectory().getPath()是“/storage/emulated/0/”左右的形式。

您将其拆分以获取根-

String media_path = Environment.getExternalStorageDirectory().getPath();
String[] splitPath= media_path .split("/");
final String MEDIA_PATH = "/" + splitPath[1] + "/";

这行得通!

于 2016-03-10T15:45:38.373 回答
-1

问题在于android认为是外部存储。您添加到设备中的并不总是 sd 卡。内部存储器也可以是外部存储器。/sdcard 或 /mnt/sdcard 是同一位置,它可能指向您的内部存储。所以你必须彻底扫描 /mnt 文件夹。就像在某些设备中 /mnt/sdcard 将是内部存储器,而 /mnt/extSdCard 将是您添加的那个。文件主页 = Environment.getExternalStorageDirectory(); 此方法将返回第一个外部存储,而不是全部。

于 2015-08-21T00:50:42.107 回答
-2

试试这个:final String MEDIA_PATH = "/storage/";

于 2015-06-07T09:58:09.077 回答