Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 880
max59 Avatar asked Jul 10 '13 14:07

max59


3 Answers

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.

like image 178
Ken Wolf Avatar answered Oct 23 '22 03:10

Ken Wolf


If you have album ID you get Album Image uri :-

final public static Uri sArtworkUri = Uri
            .parse("content://media/external/audio/albumart");      

Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
                listOfAlbums.get(position).getAlbumID());

And if you have a Image uri you can use any of the image loader Glide, Picaso, UIL to display images .

                               **OR**

you can write your own image loader

public Bitmap getAlbumart(Context context, Long album_id) {
    Bitmap albumArtBitMap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {

        final Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");

        Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

        ParcelFileDescriptor pfd = context.getContentResolver()
                .openFileDescriptor(uri, "r");

        if (pfd != null) {
            FileDescriptor fd = pfd.getFileDescriptor();
            albumArtBitMap = BitmapFactory.decodeFileDescriptor(fd, null,
                    options);
            pfd = null;
            fd = null;
        }
    } catch (Error ee) {
    } catch (Exception e) {
    }

    if (null != albumArtBitMap) {
        return albumArtBitMap;
    }
    return getDefaultAlbumArtEfficiently(context.getResources());
}



public Bitmap getDefaultAlbumArtEfficiently(Resources resource) {

    if (defaultBitmapArt == null) {

        defaultBitmapArt = decodeSampledBitmapFromResource(resource,
                R.drawable.default_album_art, UtilFunctions
                        .getUtilFunctions().dpToPixels(85, resource),
                UtilFunctions.getUtilFunctions().dpToPixels(85, resource));

    }
    return defaultBitmapArt;
}
like image 24
Hitesh Sahu Avatar answered Oct 23 '22 01:10

Hitesh Sahu


 ContentResolver musicResolve = getContentResolver();
    Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    Cursor music =musicResolve.query(smusicUri,null         //should use where clause(_ID==albumid)
        ,null, null, null);



    music.moveToFirst();            //i put only one song in my external storage to keep things simple
    int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
        String thisArt = music.getString(x);


     Bitmap bm= BitmapFactory.decodeFile(thisArt);
        ImageView image=(ImageView)findViewById(R.id.image);
        image.setImageBitmap(bm);
like image 25
Kishan Kumar Avatar answered Oct 23 '22 01:10

Kishan Kumar