How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART?

max59 picture max59 · Jul 10, 2013 · Viewed 43.1k times · Source

I'm trying to build a MP3 player and I want the ImageView to display the album art of respective songs. I've tried the following, but it doesn't work.

albumcover = (ImageView) findViewById(R.id.cover);

String coverPath = songsList.get(songIndex).get(MediaStore.Audio.Albums.ALBUM_ART);
Drawable img = Drawable.createFromPath(coverPath);
albumcover.setImageDrawable(img);

When I try to play the songs, all I get is a blank screen in the ImageView.

Answer

Ken Wolf picture Ken Wolf · Jul 10, 2013

Here's how I get album art for a song:

Cursor cursor = 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(albumId)}, 
                null);

if (cursor.moveToFirst()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    // do whatever you need to do
}

albumId refers to MediaStore.Audio.Media.ALBUM_ID for that song.

If you're are looking for album art for a particular song (rather than in a list of albums), as far as I know it's a two-stage process since ALBUM_ART is a property of MediaStore.Audio.Albums and is not available directly as song metadata.