[英]MediaStore.Audio.Media.EXTERNAL_CONTENT_URI is deprecated for api 29+?
I am developing an offline music player application.我正在开发一个离线音乐播放器应用程序。 For this, I get the data of the music with MediaStore.Audio.Media.DATA and a similar structure and list them on the recycler view.
为此,我使用 MediaStore.Audio.Media.DATA 和类似结构获取音乐数据,并将它们列在回收站视图中。 The code works fine up to api level 29, but not working api 29 and later.
该代码在 api 级别 29 之前工作正常,但在 api 29 及更高版本中无法正常工作。 On the Google MediStore Documents page, it is written that the MediaStore library is deprecated from api 29+ and later.
在 Google MediStore 文档页面上,写到 MediaStore 库已从 api 29+ 及更高版本中弃用。 So what should I do for a system running after 29+?
那么对于 29+ 之后运行的系统我应该怎么做呢? I couldn't find a source or sample code for it.
我找不到它的源代码或示例代码。 If I can solve this, I will also share the Github link of my project.
如果我能解决这个问题,我也会分享我项目的 Github 链接。 I am a Junior developer, I will be very happy if anyone can help or suggest resources.The method I wrote that works without api 29 and before;
我是一名初级开发人员,如果有人可以提供帮助或建议资源,我将非常高兴。我编写的方法在没有 api 29 及之前版本的情况下有效;
public void loadData() {
ContentResolver contentResolver = requireContext().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = TITLE + " ASC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
model = new ArrayList<>();
while (cursor.moveToNext()) {
@SuppressLint("Range") String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));@SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE));
@SuppressLint("Range") String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
@SuppressLint("Range") String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
@SuppressLint("Range") String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
@SuppressLint("Range") String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
@SuppressLint("Range") String volume = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.VOLUME_NAME));
@SuppressLint("Range") String bucketName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.BUCKET_DISPLAY_NAME));
// Save to audioList
int calculatedDuration = (Integer.valueOf(duration) / 60);
model.add(new MusicModel(data, title, album, artist));
Log.e("test",
"\n data = " + data +
"\n" + "title = " + title +
"\n" + "album = " + album +
"\n" + "artist = " + artist +
"\n" + "duration = " + calculatedDuration +
"\n" + "display name = " + displayName +
"\n" + "volume name = " + volume +
"\n" + "bucket name = " + bucketName);
}
}
cursor.close();
ArtistAdapter adapter = new ArtistAdapter(requireContext(), model);
binding.rvArtist.setAdapter(adapter);
binding.rvArtist.setHasFixedSize(true);
}
so this code works perfectly until api 29. but I don't know of an alternative that I can use on 29+ and later.所以这段代码在 api 29 之前都可以正常工作。但我不知道我可以在 29+ 及更高版本上使用的替代方案。 What should I do?
我应该怎么办?
@blackapps I re-read the MediaStore page and I think my code works only when the song I'm trying to read at some api level is only in internal storage. @blackapps我重新阅读了MediaStore页面,我认为我的代码仅在我试图以api级别阅读的歌曲仅在内部存储中时才有效。 When I moved the songs into internal storage in the emulator (api 33, api 29), the code worked without any problems.
当我将歌曲移动到模拟器(api 33,api 29)的内部存储中时,代码运行没有任何问题。 I realized when you said that this problem is not caused by the library I'm using, currently my code works fine on all api levels except 28. I think it is a problem independent of the MediaStore library, but in general, my problem is solved.
当你说这个问题不是由我正在使用的库引起时,我意识到,目前我的代码在除 28 之外的所有 api 级别上都可以正常工作。我认为这是一个独立于 MediaStore 库的问题,但总的来说,我的问题是解决了。 Thank you very much, your comment really helped me realize it.
非常感谢你,你的评论真的让我意识到了这一点。 I am sharing on my profile the github link of my project, but sorry if I made a mistake as I am new to using android studio and github.
我在我的个人资料上分享了我的项目的 github 链接,但如果我犯了错误,我很抱歉,因为我是使用 android 工作室和 github 的新手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.