Get cover picture by song

跟風遠走 提交于 2019-11-26 17:21:19

问题


Is it possible to get a cover picture by song and not by album. Because I have one self combined album with songs and they all have different cover pictures. But when I want to query them I always get the same picture returned.

String[] ARG_STRING = {MediaStore.Audio.Media.ALBUM_ID};
...
String albumCover = _cursor.getString(_cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
...
MusicUtils.getArtwork(this, -1, Integer.parseInt(albumID));

So i would like to know how it's possible to get an cover image of an song.

I know MusicUtils supports getArtwork by SongId, but what ID should I use because MediaStore.Audio.Media._ID is not working.


回答1:


I'm not familiar with MusicUtils, however, you should be able to get the cover art from the file itself by using MediaMetadataRetriever. Here is a brief code snippet showing how to use it. The uri referenced is the content uri for the file you want to retrieve the art for.

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();

mmr.setDataSource(getApplicationContext(), uri);
rawArt = mmr.getEmbeddedPicture();

// if rawArt is null then no cover art is embedded in the file or is not 
// recognized as such.
if (null != rawArt) 
    art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);

// Code that uses the cover art retrieved below.


来源:https://stackoverflow.com/questions/15314617/get-cover-picture-by-song

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!