【问题标题】:how to catch all songs in sdcard and also sdcard folders like music and etc如何捕捉 sdcard 中的所有歌曲以及音乐等 sdcard 文件夹中的所有歌曲
【发布时间】:2018-10-05 14:46:32
【问题描述】:

我使用此代码查找 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 中的音乐

【问题讨论】:

    标签: android path mp3 filefilter


    【解决方案1】:

    要获取所有 mp3 歌曲,您必须使用媒体存储数据库,它将列出设备的所有歌曲

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

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多