'Android Studio - Trying to Read MP3 FIles from the Music Directory on Android Device

I have a function below that I got from a post on here, where I am trying to read the music files from the device.

   public List<AudioModel> getAllAudioFromDevice(final Context context) {
        final List<AudioModel> tempAudioList = new ArrayList<>();
        Cursor c = getAllTracks(this);


        if (c != null) {
            Log.d("Count :", String.valueOf(c.getCount()));
            while (c.moveToNext()) {
                AudioModel audioModel = new AudioModel();
                String path = c.getString(0);
                String name = c.getString(1);
                String album = c.getString(4);

                audioModel.setaAlbum(album);
                audioModel.setaArtist(artist);
       
                Log.e("Name :" + name, " Album :" + album);
                Log.e("Path :" + path, " Artist :" + artist);

                tempAudioList.add(audioModel);
            }
            c.close();
        } else {
            Log.d("Name :", "No Music");
        }

        return tempAudioList;
    }

    public Cursor getAllTracks(Context context) {
        // gets all tracks
        if (context != null) {
            ContentResolver cr = context.getContentResolver();
            final String[] columns = {track_id, track_no, artist, track_name,
                    album, duration, path, year, composer};
            return cr.query(uri, columns, null, null, null);
        } else {
            return null;
        }
    }

When I run the app, it doesn't list any of the MP3 files, but it picks up oog files. The mp3 files I have uploaded to the emulator look as below:

Emulator storage image one

Emulator storage image two

I have tried the EXTERNAL_CONTENT_URI as well, but that doesn't display them.

This is from the "Playlist Manager" app as suggested by Theo:

enter image description here

Sorry, I am very new to Android Studio and am probably missing something very obvious. Can someone help please?



Solution 1:[1]

Your issue is

Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

change this to

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

and also remove the .DATA bit from Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA, null, null) as you put criteria there. Look up the syntax for query. Set it to null and it will work

Solution 2:[2]

This is my approach, note the syntax of

return cr.query(uri, columns, null, null, null)

whereas you have

  Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA, null, null);



 private final String track_id = MediaStore.Audio.Media._ID;
    private final String track_no = MediaStore.Audio.Media.TRACK;
    private final String track_name = MediaStore.Audio.Media.TITLE;
    private final String artist = MediaStore.Audio.Media.ARTIST;
    private final String artist_id = MediaStore.Audio.Media.ARTIST_ID;
    private final String duration = MediaStore.Audio.Media.DURATION;
    private final String album = MediaStore.Audio.Media.ALBUM;
    private final String composer = MediaStore.Audio.Media.COMPOSER;
    private final String year = MediaStore.Audio.Media.YEAR;
    private final String path = MediaStore.Audio.Media.DATA;
    private final String date_added = MediaStore.Audio.Media.DATE_ADDED;
    private final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;


        public Cursor getAllTracks(Context context) {
        // gets all tracks
        if (context != null) {
            ContentResolver cr = context.getContentResolver();
            final String[] columns = {track_id, track_no, artist, track_name,
                    album, duration, path, year, composer};
            return cr.query(uri, columns, null, null, null);
        } else {
            return null;
        }
    }

Solution 3:[3]

For what it's worth - I had the exact same problem. I added my audio files to the emulator using the Device File Explorer and the code didn't seem to work. It turns out that I had to simply reboot my emulator after adding the files in order for the uploaded files to appear in the MediaStore data base. I don't know if this is a bug or a feature in Android Studio and/or the emulator, but in my case it fixed the issue. I tested it on two different emulator sessions (one with SDK31 and the other SDK29).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Theo
Solution 3