乐如网功夫战警_乐如网_天天快消息

互联网 2023-06-27 07:09:03


【资料图】

1、该图片为使用如下代码解析得到: 参考源码中,音乐目录: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============================");}这是抽取专辑图片的方法,扫描歌曲的时候自己把歌曲的id和专辑的id传进去就能获取private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");/*** 将MP3里图片读取出来* * @param context* @param songid* @param albumid* @return*/public static Bitmap getMusicBitemp(Context context, long songid,long albumid) {Bitmap bm = null;// 专辑id和歌曲id小于0说明没有专辑、歌曲。

2、并抛出异常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);} else {return null;}}} catch (FileNotFoundException ex) {}return bm;}。

本文到此分享完毕,希望对大家有所帮助。

x

热门推荐

乐如网功夫战警_乐如网_天天快消息

2023-06

袖手尘嚣歌曲原唱_袖手尘嚣

2023-06

魔兽世界宠物升级攻略图文 魔兽世界宠物升级攻略

2023-06

狗狗肠梗阻怎么办_肠梗阻怎么办_每日视讯

2023-06

雷环一个人资料_雷环-聚看点

2023-06

环球快消息!无法初始化选定的扫描仪光学分辨率不可用-无法初始化选定的扫描仪

2023-06

当前速递!俄国防部:乌克兰利用普里戈津“挑衅”的机会发动反攻

2023-06

a到g罩杯变大_a到g罩杯大小示意图-焦点速看

2023-06

全球简讯:倾冷月小说(倾冷月)

2023-06

上海闵行区养老院一览表

2023-06

推荐阅读

深圳发布养老服务合同示范文本规范养老机构服务行为

2022-03

山东深耕文化资源 推动旅游业高质量发展

2021-12

安徽潜山两车相撞 已致8人死亡3人受伤

2021-12

56位残疾人士登上黄山 互利互勉共建生活希望

2021-12

山西忻州古城:一城风华延续千年历史文脉

2021-12

上海洋山海关首次在出口货运渠道查获夹带卷烟

2021-12

西藏林芝:多彩民俗活动迎接工布新年

2021-12

呼伦贝尔新巴尔虎右旗公布1例无症状感染者行动轨迹

2021-12

新增“53+1” 内蒙古累计本土确诊病例增至185例

2021-12

面对儿童不得不防的“杀手”,这把椅子很重要!

2021-12