2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > android图片音乐播放器 android 音乐播放器----获取专辑封面图片

android图片音乐播放器 android 音乐播放器----获取专辑封面图片

时间:2019-10-23 00:47:46

相关推荐

android图片音乐播放器 android 音乐播放器----获取专辑封面图片

本文来自CSDN丹丹博客,转载请必须注明出处:

/dany1202/archive//06/08/6532024.aspx

.mp3歌曲中,附带有专辑封面的图片,如何解析获取并显示这张图片呢?如图:

该图片为使用如下代码解析得到:

参考源码中,音乐目录:

packages/apps/Music/src/com/android/music/MusicUtils.java中函数:getArtwork(context, song_id, album_id, true)

public static Bitmap getArtwork(Context context, long song_id, long album_id,

boolean allowdefault) {

if (album_id < 0) {

// This is something that is not in the database, so get the album art directly

// from the file.

if (song_id >= 0) {

Bitmap bm = getArtworkFromFile(context, song_id, -1);

if (bm != null) {

return bm;

}

}

if (allowdefault) {

return getDefaultArtwork(context);

}

return null;

}

ContentResolver res = context.getContentResolver();

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

if (uri != null) {

InputStream in = null;

try {

in = res.openInputStream(uri);

return BitmapFactory.decodeStream(in, null, sBitmapOptions);

} catch (FileNotFoundException ex) {

// The album art thumbnail does not actually exist. Maybe the user deleted it, or

// maybe it never existed to begin with.

Bitmap bm = getArtworkFromFile(context, song_id, album_id);

if (bm != null) {

if (bm.getConfig() == null) {

bm = bm.copy(Bitmap.Config.RGB_565, false);

if (bm == null && allowdefault) {

return getDefaultArtwork(context);

}

}

} else if (allowdefault) {

bm = getDefaultArtwork(context);

}

return bm;

} finally {

try {

if (in != null) {

in.close();

}

} catch (IOException ex) {

}

}

}

return null;

}

private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {

Bitmap bm = null;

byte [] art = null;

String path = null;

if (albumid < 0 && songid < 0) {

throw new IllegalArgumentException("Must specify an album or a song id");

}

try {

if (albumid < 0) {

Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");

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

if (pfd != null) {

FileDescriptor fd = pfd.getFileDescriptor();

bm = BitmapFactory.decodeFileDescriptor(fd);

}

} else {

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

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

if (pfd != null) {

FileDescriptor fd = pfd.getFileDescriptor();

bm = BitmapFactory.decodeFileDescriptor(fd);

}

}

} catch (FileNotFoundException ex) {

}

if (bm != null) {

mCachedBit = bm;

}

return bm;

}

private static Bitmap getDefaultArtwork(Context context) {

BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inPreferredConfig = Bitmap.Config.RGB_565;

return BitmapFactory.decodeStream(

context.getResources().openRawResource(R.drawable.play_img_default), null, opts);

}

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

private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();

private static Bitmap mCachedBit = null;

获取cursor:

myCur = getContentResolver().query(

MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,

new String[] { MediaStore.Audio.Media.TITLE,

MediaStore.Audio.Media.DURATION,

MediaStore.Audio.Media.ARTIST,

MediaStore.Audio.Media._ID,

MediaStore.Audio.Media.ALBUM,

MediaStore.Audio.Media.DISPLAY_NAME,

MediaStore.Audio.Media.DATA,

MediaStore.Audio.Media.ALBUM_ID}, null,null, null);

myCur.moveToPosition(position);

设置专辑封面图片:

long songid = myCur.getLong(3);

long albumid = myCur.getLong(7);

Bitmap bm = MusicUtils.getArtwork(this, songid, albumid,true);

if(bm != null){

Log.d(TAG,"bm is not null==========================");

playImg.setImageBitmap(bm);

}else{

Log.d(TAG,"bm is null============================");

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。