0

I am getting all the Strings I need from the Audio but I cant seem to get the Album Cover Image. I have tried getting it with MediaStore.Audio.Albums.ALBUM_ART but it does not give me anything. What am I doing wrong?

public class AudioManager {

public static ArrayList<Audio> LoadAudioForSongList(Activity activity) {

    ArrayList<Audio> audioList = new ArrayList<>();

    ContentResolver contentResolver = activity.getContentResolver();

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

    if (cursor != null && cursor.getCount() > 0) {

        while (cursor.moveToNext()) {

            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

            //Album art
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

            // Save to audioList
            audioList.add(new Audio(data, title, album, artist, duration, url));
        }
    }

    if (cursor != null) {
        cursor.close();
    }
    return audioList;
}
}
Richard
  • 1,087
  • 18
  • 52
  • 1
    Have a look here https://stackoverflow.com/questions/50423453/check-if-album-art-exists/50428644#50428644 – Theo Jun 18 '18 at 14:44
  • I dont really understand what all the variables are there and how I could use it here in this class. – Richard Jun 18 '18 at 17:41
  • 1
    all you need is the albumid. Once you have that you can get the uri as shown in the second code snippet. Once you have the uri, use the Glide library to display albumart. It is far easier than trying to get the bitmap yourself (search for BitmapFactory) . Also getting the bitmaps sometimes results in OOM (out of memory) crashes. Glide does it all for you – Theo Jun 18 '18 at 19:13
  • @Theo So this will work if I get the `MediaStore.Audio.Media.ALBUM_ID` ? – Richard Jun 18 '18 at 19:27
  • Also isn't your code searching for the album art from other folder not the audio itself. And what is `plist` – Richard Jun 18 '18 at 19:32
  • 1
    Yes, it will work. You do not want to read the album art from the track itself as it will be too slow. Just ignore the plist as it represents one of my classes – Theo Jun 19 '18 at 06:26
  • I was having problems because someone else had used `Glide 3.7` in my project so your suggested method didnt work, but once I figured it out, I got it to work. Thanks man. – Richard Jun 19 '18 at 20:54
  • @kararoty thank you for your respones. – Theo Jun 20 '18 at 13:00

0 Answers0