1

I am creating a Media player app which gets the songs and their data from the users phone add them in an ArrayList. Im reading the data through cursors and im kinda stuck at the Album Art. The code im using below does not crash the app and even if the mp3s stored in my device have album art i cannot get em. COuld use some help

Cursor imgCursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                            new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                            MediaStore.Audio.Albums._ID + "=?",
                            new String[]{String.valueOf(MediaStore.Audio.Albums._ID)},
                            null);
                    do{
                    if (imgCursor.moveToFirst()) {
                        String imgPath = imgCursor.getString(imgCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                        Log.v("Musiclistactivity", "img path is" + imgPath);
                        currentImage = BitmapFactory.decodeFile(imgPath);
                    } else {
                        currentImage = BitmapFactory.decodeResource(getResources(), R.drawable.nocover);
                    }}while (imgCursor.moveToNext()) ;  

this is what im getting enter image description here

Pointyhat
  • 465
  • 1
  • 4
  • 18

2 Answers2

0

My suggestion is to forget about BitmapFatcory etc. even when you do sort it, it will encounter Out Of Memory errors. I am using the Glide library. This takes care of everything and is very easy to apply. Look at my answer in this link

Gilde to manage albumart

Theo
  • 2,012
  • 1
  • 16
  • 29
  • I use picasso which is similar to Glide, but do you have any idea how i can sort the albums? i display album name and album art in a fragment, but when i sort the album names, it doesn't match the album art. How can i fix this? – Vince VD Jul 01 '18 at 01:38
  • I made a post which explains my problem a bit more detailed https://stackoverflow.com/questions/51094052/how-can-i-sort-albums-from-songs – Vince VD Jul 01 '18 at 01:39
0

I dit it like this...


Glide.with(imageView.getContext())
                .load(getSongCover(file.getPath()))
                .placeholder(R.drawable.audio_placeholder)
                .error(R.drawable.audio_placeholder)
                .dontAnimate()
                .into(imageView);

public byte[] getSongCover(String path) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(path);
        return mmr.getEmbeddedPicture();
    }

add it to your build.gradle

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

works great inside recyclerview, listview etc.

J El
  • 100
  • 7