28

I am developing a sort of media player for android. The question is how can i get the cover art of audio file on android. For example the default android media player shows album covers when listing albums, how can i get this artworks.

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • 1
    you can also try this: http://stackoverflow.com/questions/10209176/extract-album-cover-from-mp3-file-in-android?rq=1 – Chepech Jan 22 '14 at 16:47

7 Answers7

49
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);

More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.

Fedor
  • 43,261
  • 10
  • 79
  • 89
10

You can use

MediaMetadataRetriever

class and get track info i.e. Title,Artist,Album,Image

Bitmap GetImage(String filepath)              //filepath is path of music file
{
 Bitmap image;

MediaMetadataRetriever mData=new MediaMetadataRetriever();
mData.setDataSource(filePath);
     try{
            byte art[]=mData.getEmbeddedPicture();
            image=BitmapFactory.decodeByteArray(art, 0, art.length);
        }
    catch(Exception e)
        { 
           image=null;
        }
    return image;
}
Altaf Shaikh
  • 729
  • 8
  • 19
9

I don't know why everybody is making it so complicated you can use Glide to achieve this in simplest and efficient way with just 1 line of code

Declare this path in App Constants -

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

Get Image Uri using Album ID

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

Now simply display album art using uri :-

    Glide.with(context).load(uri).placeholder(R.drawable.art_default).error(R.drawable.art_default)
                    .crossFade().centerCrop().into(holder.albumImage);

Glide will handle caching, scaling and lazy loading of images for you.

Hope it help somebody.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • I am getting content://media/external/audio/albumart/3 this Uri as output which is not showing any image. – M Umer Feb 22 '20 at 15:25
3

Here i can attach one function that is return album art from media store .

Here in function we just have to pass the album_id which we get from Media store .

public Bitmap getAlbumart(Long album_id) 
   {
        Bitmap bm = null;
        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();
                bm = BitmapFactory.decodeFileDescriptor(fd);
            }
    } catch (Exception e) {
    }
    return bm;
}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
2

Based on your comments to others, it seems like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as Nick has suggested, I believe you should be able to retrieve it the way Fudgey suggested.

Tim Kryger
  • 11,166
  • 4
  • 52
  • 41
  • No i don't want to get the album art in general. There is optional filed in ID3v2 tags for cover art. Android is parsing most of the ID3v2 field and the question is how to get the embedded covert art in ID3v2 tag not using amazon or some other web services – Mojo Risin Jan 03 '10 at 15:02
  • 1
    Maybe the question is little confusing, maybe the correct one will be extracting "Attached picture" from ID3v2 tag on android. http://www.id3.org/id3v2.3.0 for more info about attached picture – Mojo Risin Jan 03 '10 at 15:06
0

Android only recognizes files named "AlbumArt.jpg" as Album Covers. Just put the pictures with that name in the album folder and you'll be fine..

NiklasMM
  • 2,895
  • 2
  • 22
  • 26
  • 1
    Sorry maybe the question was little confusing. I am developing an application and i want to get the album art from my application. – Mojo Risin Dec 23 '09 at 18:26
0

I don't know if you read that google is making Stackoverflow the official Android app development Q&A medium but for beginner questions... Now, I know nothing about developing in andriod, but a quick search of the Android Developers site, I found this:

MediaStore.Audio.AlbumColumns

Hopefully it'll help.

Mottie
  • 84,355
  • 30
  • 126
  • 241
  • 1
    Yes i also notice that there is MediaStore.Audio.AudioColumns.ALBUM_ART but it doesn't exist actually.ContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null, null, null, null); - returns cursor with all columns and ALBUM_ART is not there. – Mojo Risin Dec 23 '09 at 22:19
  • 1
    There are only ALBUM,ALBUM_ID,ALBUM_KEY columns in the cursor but no ALBUM_ART – Mojo Risin Dec 23 '09 at 22:28