MediaStore.Audio.Media.EXTERNAL_CONTENT_URI is deprecated for api 29+?

Viewed 11

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?

0 Answers
Related