网上转的都太不靠谱了 = = 在一个MP3播放器里面挖了出来,用时就用上了~
1 public static Bitmap getAlbumArtWork(String filePath) { 2 try { 3 MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever(); 4 metaRetriver.setDataSource(filePath); 5 byte[] album = metaRetriver.getEmbeddedPicture(); 6 if (album != null) { 7 BitmapFactory.Options opts = new BitmapFactory.Options(); 8 opts.inJustDecodeBounds = true; 9 BitmapFactory.decodeByteArray(album, 0, album.length, opts); 10 opts.inSampleSize = calculateInSampleSize(opts); 11 opts.inJustDecodeBounds = false; 12 return BitmapFactory.decodeByteArray(album, 0, album.length, opts); 13 } 14 return null; 15 } catch (Exception e) { 16 return null; 17 } 18 } 19 public static int calculateInSampleSize(BitmapFactory.Options options) { 20 // Raw height and width of image 21 final int height = options.outHeight; 22 final int width = options.outWidth; 23 int inSampleSize = 1; 24 //Our height and width will always be the same since all glass has the same resolution, for now... 25 if (height > 360 || width > 640) { 26 final int halfHeight = height / 2; 27 final int halfWidth = width / 2; 28 // Calculate the largest inSampleSize value that is a power of 2 and keeps both 29 // height and width larger than the requested height and width. 30 while ((halfHeight / inSampleSize) > 360 && (halfWidth / inSampleSize) > 640) { 31 inSampleSize *= 2; 32 } 33 } 34 35 return inSampleSize; 36 }