开发者

cover art on android

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开发者_StackOverflow中文版 example the default android media player shows album covers when listing albums, how can i get this artworks.


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.


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;
}


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.


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;
}


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.


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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜