如何使用MediaStore获取特定目录和所有子目录中的所有音乐列表

时间:2016-10-09 13:04:11

标签: java android mediastore

我需要的是从此代码获得结果

ContentResolver musicResolver = getActivity().getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

但是在特定目录中,而不是在所有目录中。 这是我到目前为止所尝试的:

ContentResolver musicResolver = mContext.getContentResolver();
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = null;
    try {
        musicCursor = musicResolver.query(uri,
                new String[]{
                        MediaStore.Audio.Media._ID,
                        MediaStore.Audio.Media.ARTIST,
                        MediaStore.Audio.Media.TITLE,
                        MediaStore.Audio.Media.DURATION,
                },
                MediaStore.Audio.Media.DATA + "=? ",
                new String[]{new File("some path to directory").getCanonicalPath()},
                null);
    } catch (IOException e) {
        e.printStackTrace();
    }

但是这种方法moveToFirst()  返回false,因为我知道它是空的。 感谢您将来的帮助。

1 个答案:

答案 0 :(得分:0)

   searchpath = "%" + yourpath + "%";// looking for path string
   ContentResolver musicResolver = mContext.getContentResolver();
  Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  Cursor musicCursor = null;
  try {
    musicCursor = musicResolver.query(uri,
            new String[]{
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.DURATION,
            },
            MediaStore.Audio.Media.DATA + "LIKE? ",
            new String[]{searchpath},
            null);
} catch (IOException e) {
    e.printStackTrace();
}