使用MediaStore列出特定路径下的媒体文件

4

我正在尝试从特定文件夹获取mp3文件列表。

目前,我正在使用contentResolver.query来获取手机上的音乐。

像这样:

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION
        };

        ContentResolver contentResolver = context.getContentResolver();
        cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);

我正在寻找一种方法来仅扫描特定文件夹。如何使用contentResolver实现这个功能?

谢谢!


使用ContentResolver和正则表达式从特定文件夹获取音乐。 - Xiangshi Yin
1个回答

4

最终我自己解决了这个问题。希望能帮到其他人。

不要这样做:

cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

我使用了这个:

cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
                new String[] {"%MyMusicFolder%"},  null);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接