如何捕捉sdcard中的所有歌曲以及音乐等的sdcard文件夹

时间:2018-04-25 07:58:42

标签: android path mp3 filefilter

我使用此代码查找mp3文件:

`

   // String extStore = System.getenv("EXTERNAL_STORAGE");
   // File home = new File(extStore);


    //String extStore = "/storage/extSdCarcd";
    String extStore = "/storage/";
    File home = new File(extStore);

    if(home.listFiles(new FileExtensionFilter()).length>0){

        for(File file : home.listFiles(new FileExtensionFilter())){

            HashMap<String,String> song = new HashMap<String, String>();
            song.put("title",file.getName().substring(0,(file.getName().length()-4)));
            song.put("path",file.getPath());
            songsList.add(song);
        }
    }

    return songsList;
}`

如你所见,我尝试了很多方法来获取.mp3文件,但如果我的蓝牙文件夹或音乐文件夹中有mp3文件,他们就不会有帮助。它们只适用于sdcard中的音乐

1 个答案:

答案 0 :(得分:0)

获取所有mp3歌曲你必须使用媒体商店db,它将列出设备的所有歌曲

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


public ArrayList<HashMap<String, String>> getPlayList(Context c) {

    final Cursor mCursor = c.getContentResolver().query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        new String[] { MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM }, null, null,
        "LOWER(" + MediaColumns.TITLE + ") ASC");

    String songTitle = "";
    String songPath = "";

    /* run through all the columns we got back and save the data we need into the arraylist for our listview*/
    if (mCursor.moveToFirst()) {
        do {
            songTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));
            songPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.DATA));

            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", songTitle);
            song.put("songPath", songPath);
            songsList.add(song);

        } while (mCursor.moveToNext());
    }   

    mCursor.close(); //cursor has been consumed so close it
    return songsList;
}