列表中的所有 Mp3 文件android

发布时间: 2016/9/28 0:43:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在开发中的音乐播放器 android 但卡在读取 MP3 文件.这里是我的代码来读取归来任何文件从设备 (虽然有一些文件,我复制使用亚行)。 所有 mp3 文件。 但它不我也希望它可以列出使用专辑、 艺术家等请帮助在这。

final String MEDIA_PATH = Environment.getExternalStorageDirectory()+"";

private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);

    //if (home.listFiles(new FileExtensionFilter()).length > 0) {
    if (home.listFiles(new FileExtensionFilter())!=null) {

        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 to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

解决方法 1:

在这里我已经修改了您的 getPlayList() 方法。望着。

ArrayList<HashMap<String,String>> getPlayList(String rootPath) {
            ArrayList<HashMap<String,String>> fileList = new ArrayList<>();


            try {
                File rootFolder = new File(rootPath);
                File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains  any file,handle it like this.
                for (File file : files) {
                    if (file.isDirectory()) {
                        if (getPlayList(file.getAbsolutePath()) != null) {
                            fileList.addAll(getPlayList(file.getAbsolutePath()));
                        } else {
                            break;
                        }
                    } else if (file.getName().endsWith(".mp3")) {
                        HashMap<String, String> song = new HashMap<>();
                        song.put("file_path", file.getAbsolutePath());
                        song.put("file_name", file.getName());
                        fileList.add(song);
                    }
                }
                return fileList;
            } catch (Exception e) {
                return null;
            }
        }

你可以得到的歌曲名称和歌 path 像这样︰

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ArrayList<HashMap<String,String>> songList=getPlayList("/storage/sdcard1/");
        if(songList!=null){
        for(int i=0;i<songList.size();i++){
        String fileName=songList.get(i).get("file_name");
        String fileName=songList.get(i).get("file_path");
        //here you will get list of file name and file path that present in your device
        log.e("file details "," name ="+fileName +" path = "+filePath);
        }
        }
    }

注︰使用"/ 存储/sdcard1 /"从 sd 卡和使用 Environment.getExternalStorageDirectory().getAbsolutePath() 从手机内存读取文件读取文件

希望这将帮助你。

赞助商