Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display album art?

Tags:

android

I want to know how to display album art for albums using android.provider.MediaStore.Audio.Albums.ALBUM.Album_Art.

I am extracting metadata from the path by using the following code which works fine for songs, but I just don't know how to display album art for albums/artists in general.

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt = null;
float ht_px = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
BitmapFactory.Options bfo=new BitmapFactory.Options();
try {
    mmr.setDataSource(songdetails.get(swapnumber).Path);
    StackBlurManager _stackBlurManager;
    rawArt = mmr.getEmbeddedPicture();
    if ( rawArt != null)  { 
        bitmap2 = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
        bitmap3 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);
//...
like image 597
Ankit Srivastava Avatar asked Nov 20 '13 06:11

Ankit Srivastava


1 Answers

First I would recommend having a look at:

  • Android SQLite database and content provider
  • Loaders and Loader Manager Background

This code snippet may help get you going in the right direction.

// Query URI
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

// Columns
String[] select = {
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.ALBUM,
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.ALBUM_ID,
    MediaStore.Audio.Media.DURATION
};

// Where
String where = MediaStore.Audio.Media.IS_MUSIC + "=1";

// Perform the query
Cursor cursor = context.getContentResolver().query(uri, cursor_cols, where, null, null);

if (cursor.moveToFirst()) {
    while (!cursor.isAfterLast()) {
        long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
        String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
        String track = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
        String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
        int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

        final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);

        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
        } catch (Exception exception) {
            // log error
        }

        cursor.moveToNext();
    }
}
like image 124
Tanner Perrien Avatar answered Oct 29 '22 07:10

Tanner Perrien