MediaStore.Audio和ContentResolver.query未获取音乐



我在Android Studio中有一个音乐播放器项目,我需要从设备中获取所有音乐
但不知何故,this.getContentResolver().query();无法从我的设备中获取任何音乐
在logcat中没有日志,如果我在while循环之前和之后调试应用程序(因为它没有进入while循环(,它会说光标没有任何行(mCount=0(

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = this.getContentResolver();
String[] projection = {
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST
};
String where = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor c = resolver.query(uri, projection, where, null, null);
if (c != null) {
while (c.moveToNext()) {
String path = c.getString(0);
String name = c.getString(1);
String album = c.getString(2);
String artist = c.getString(3);
Log.e("Name :" + name, " Album :" + album);
Log.e("Path :" + path, " Artist :" + artist);
}
c.close();
}

也许,在获取音频之前需要请求权限。

我希望这个答案能帮助你。https://stackoverflow.com/a/77041318/6631835

最新更新