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

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

http://blog.csdn.net/dany1202/archive/2011/06/08/6532024.aspx

 

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

            

 

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

 

参考源码中,音乐目录:

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

[java]  view plain copy
  1. public static Bitmap getArtwork(Context context, long song_id, long album_id,  
  2.             boolean allowdefault) {  
  3.         if (album_id < 0) {  
  4.             // This is something that is not in the database, so get the album art directly  
  5.             // from the file.  
  6.             if (song_id >= 0) {  
  7.                 Bitmap bm = getArtworkFromFile(context, song_id, -1);  
  8.                 if (bm != null) {  
  9.                     return bm;  
  10.                 }  
  11.             }  
  12.             if (allowdefault) {  
  13.                 return getDefaultArtwork(context);  
  14.             }  
  15.             return null;  
  16.         }  
  17.         ContentResolver res = context.getContentResolver();  
  18.         Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);  
  19.         if (uri != null) {  
  20.             InputStream in = null;  
  21.             try {  
  22.                 in = res.openInputStream(uri);  
  23.                 return BitmapFactory.decodeStream(in, null, sBitmapOptions);  
  24.             } catch (FileNotFoundException ex) {  
  25.                 // The album art thumbnail does not actually exist. Maybe the user deleted it, or  
  26.                 // maybe it never existed to begin with.  
  27.                 Bitmap bm = getArtworkFromFile(context, song_id, album_id);  
  28.                 if (bm != null) {  
  29.                     if (bm.getConfig() == null) {  
  30.                         bm = bm.copy(Bitmap.Config.RGB_565, false);  
  31.                         if (bm == null && allowdefault) {  
  32.                             return getDefaultArtwork(context);  
  33.                         }  
  34.                     }  
  35.                 } else if (allowdefault) {  
  36.                     bm = getDefaultArtwork(context);  
  37.                 }  
  38.                 return bm;  
  39.             } finally {  
  40.                 try {  
  41.                     if (in != null) {  
  42.                         in.close();  
  43.                     }  
  44.                 } catch (IOException ex) {  
  45.                 }  
  46.             }  
  47.         }  
  48.           
  49.         return null;  
  50.     }  
  51.       
  52.     private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {  
  53.         Bitmap bm = null;  
  54.         byte [] art = null;  
  55.         String path = null;  
  56.         if (albumid < 0 && songid < 0) {  
  57.             throw new IllegalArgumentException("Must specify an album or a song id");  
  58.         }  
  59.         try {  
  60.             if (albumid < 0) {  
  61.                 Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");  
  62.                 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");  
  63.                 if (pfd != null) {  
  64.                     FileDescriptor fd = pfd.getFileDescriptor();  
  65.                     bm = BitmapFactory.decodeFileDescriptor(fd);  
  66.                 }  
  67.             } else {  
  68.                 Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);  
  69.                 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");  
  70.                 if (pfd != null) {  
  71.                     FileDescriptor fd = pfd.getFileDescriptor();  
  72.                     bm = BitmapFactory.decodeFileDescriptor(fd);  
  73.                 }  
  74.             }  
  75.         } catch (FileNotFoundException ex) {  
  76.    
  77.         }  
  78.         if (bm != null) {  
  79.             mCachedBit = bm;  
  80.         }  
  81.         return bm;  
  82.     }  
  83.       
  84.     private static Bitmap getDefaultArtwork(Context context) {  
  85.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  86.         opts.inPreferredConfig = Bitmap.Config.RGB_565;          
  87.         return BitmapFactory.decodeStream(  
  88.                 context.getResources().openRawResource(R.drawable.play_img_default), null, opts);                 
  89.     }  
  90. private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");  
  91.     private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();  
  92.     private static Bitmap mCachedBit = null;  

 

获取cursor:

[java]  view plain copy
  1. myCur = getContentResolver().query(  
  2.                 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
  3.                 new String[] { MediaStore.Audio.Media.TITLE,  
  4.                         MediaStore.Audio.Media.DURATION,  
  5.                         MediaStore.Audio.Media.ARTIST,  
  6.                         MediaStore.Audio.Media._ID,  
  7.                         MediaStore.Audio.Media.ALBUM,  
  8.                         MediaStore.Audio.Media.DISPLAY_NAME,  
  9.                         MediaStore.Audio.Media.DATA,  
  10.                         MediaStore.Audio.Media.ALBUM_ID}, null,nullnull);  
  11.         myCur.moveToPosition(position);  

 

设置专辑封面图片:

[java]  view plain copy
  1. long songid = myCur.getLong(3);  
  2.         long albumid = myCur.getLong(7);  
  3.         Bitmap bm = MusicUtils.getArtwork(this, songid, albumid,true);  
  4.         if(bm != null){  
  5.             Log.d(TAG,"bm is not null==========================");  
  6.             playImg.setImageBitmap(bm);  
  7.         }else{  
  8.             Log.d(TAG,"bm is null============================");  
  9.         }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值