Google glass GDK - 通过MP3路径获取专辑图片

本文提供了一段Java代码,用于从指定路径的MP3文件中读取专辑封面图片。该方法通过使用MediaMetadataRetriever类来获取嵌入在音频文件中的图片,并通过调整采样率以适应特定分辨率。

网上转的都太不靠谱了 = = 在一个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     }

 

转载于:https://www.cnblogs.com/ch3rry/p/3871387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值