代码之家  ›  专栏  ›  技术社区  ›  Tara

如何捕获SD卡中的所有歌曲,以及SD卡文件夹中的音乐等

  •  0
  • Tara  · 技术社区  · 6 年前

    我使用此代码查找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文件,它们不会有帮助。它们仅用于SD卡中的音乐

    1 回复  |  直到 6 年前
        1
  •  0
  •   Viktor Stojanov    6 年前

    要获取所有mp3歌曲,您必须使用media store 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;
    }
    
    推荐文章