简体   繁体   中英

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. The code works fine up to api level 29, but not working api 29 and later. On the Google MediStore Documents page, it is written that the MediaStore library is deprecated from api 29+ and later. So what should I do for a system running after 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. 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;

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. 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. When I moved the songs into internal storage in the emulator (api 33, api 29), the code worked without any problems. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM