为了账号安全,请及时绑定邮箱和手机立即绑定

获取专辑封面图片及其他信息的方法

标签:
Android
  1. package com.wwj.sb.utils;  

  2.   

  3. import java.io.FileDescriptor;  

  4. import java.io.FileNotFoundException;  

  5. import java.io.IOException;  

  6. import java.io.InputStream;  

  7. import java.util.ArrayList;  

  8. import java.util.HashMap;  

  9. import java.util.Iterator;  

  10. import java.util.List;  

  11.   

  12. import android.content.ContentResolver;  

  13. import android.content.ContentUris;  

  14. import android.content.Context;  

  15. import android.database.Cursor;  

  16. import android.graphics.Bitmap;  

  17. import android.graphics.BitmapFactory;  

  18. import android.graphics.BitmapFactory.Options;  

  19. import android.net.Uri;  

  20. import android.os.ParcelFileDescriptor;  

  21. import android.provider.MediaStore;  

  22.   

  23. import com.wwj.sb.activity.R;  

  24. import com.wwj.sb.domain.Mp3Info;  

  25.   

  26. public class MediaUtil {  

  27.       

  28.     //获取专辑封面的Uri  

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

  30.   

  31.     /** 

  32.      * 用于从数据库中查询歌曲的信息,保存在List当中 

  33.      *  

  34.      * @return 

  35.      */  

  36.     public static List<Mp3Info> getMp3Infos(Context context) {  

  37.         Cursor cursor = context.getContentResolver().query(  

  38.                 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,  

  39.                 MediaStore.Audio.Media.DEFAULT_SORT_ORDER);  

  40.           

  41.         List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();  

  42.         for (int i = 0; i < cursor.getCount(); i++) {  

  43.             cursor.moveToNext();  

  44.             Mp3Info mp3Info = new Mp3Info();  

  45.             long id = cursor.getLong(cursor  

  46.                     .getColumnIndex(MediaStore.Audio.Media._ID));   //音乐id  

  47.             String title = cursor.getString((cursor   

  48.                     .getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题  

  49.             String artist = cursor.getString(cursor  

  50.                     .getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家  

  51.             String album = cursor.getString(cursor  

  52.                     .getColumnIndex(MediaStore.Audio.Media.ALBUM)); //专辑  

  53.             long albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));  

  54.             long duration = cursor.getLong(cursor  

  55.                     .getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长  

  56.             long size = cursor.getLong(cursor  

  57.                     .getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小  

  58.             String url = cursor.getString(cursor  

  59.                     .getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径  

  60.             int isMusic = cursor.getInt(cursor  

  61.                     .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐  

  62.             if (isMusic != 0) { // 只把音乐添加到集合当中  

  63.                 mp3Info.setId(id);  

  64.                 mp3Info.setTitle(title);  

  65.                 mp3Info.setArtist(artist);  

  66.                 mp3Info.setAlbum(album);  

  67.                 mp3Info.setAlbumId(albumId);  

  68.                 mp3Info.setDuration(duration);  

  69.                 mp3Info.setSize(size);  

  70.                 mp3Info.setUrl(url);  

  71.                 mp3Infos.add(mp3Info);  

  72.             }  

  73.         }  

  74.         return mp3Infos;  

  75.     }  

  76.       

  77.     /** 

  78.      * 往List集合中添加Map对象数据,每一个Map对象存放一首音乐的所有属性 

  79.      * @param mp3Infos 

  80.      * @return 

  81.      */  

  82.     public static List<HashMap<String, String>> getMusicMaps(  

  83.             List<Mp3Info> mp3Infos) {  

  84.         List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();  

  85.         for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {  

  86.             Mp3Info mp3Info = (Mp3Info) iterator.next();  

  87.             HashMap<String, String> map = new HashMap<String, String>();  

  88.             map.put("title", mp3Info.getTitle());  

  89.             map.put("Artist", mp3Info.getArtist());  

  90.             map.put("album", mp3Info.getAlbum());  

  91.             map.put("albumId", String.valueOf(mp3Info.getAlbumId()));  

  92.             map.put("duration", formatTime(mp3Info.getDuration()));  

  93.             map.put("size", String.valueOf(mp3Info.getSize()));  

  94.             map.put("url", mp3Info.getUrl());  

  95.             mp3list.add(map);  

  96.         }  

  97.         return mp3list;  

  98.     }  

  99.       

  100.     /** 

  101.      * 格式化时间,将毫秒转换为分:秒格式 

  102.      * @param time 

  103.      * @return 

  104.      */  

  105.     public static String formatTime(long time) {  

  106.         String min = time / (1000 * 60) + "";  

  107.         String sec = time % (1000 * 60) + "";  

  108.         if (min.length() < 2) {  

  109.             min = "0" + time / (1000 * 60) + "";  

  110.         } else {  

  111.             min = time / (1000 * 60) + "";  

  112.         }  

  113.         if (sec.length() == 4) {  

  114.             sec = "0" + (time % (1000 * 60)) + "";  

  115.         } else if (sec.length() == 3) {  

  116.             sec = "00" + (time % (1000 * 60)) + "";  

  117.         } else if (sec.length() == 2) {  

  118.             sec = "000" + (time % (1000 * 60)) + "";  

  119.         } else if (sec.length() == 1) {  

  120.             sec = "0000" + (time % (1000 * 60)) + "";  

  121.         }  

  122.         return min + ":" + sec.trim().substring(0, 2);  

  123.     }  

  124.       

  125.       

  126.     /** 

  127.      * 获取默认专辑图片 

  128.      * @param context 

  129.      * @return 

  130.      */  

  131.     public static Bitmap getDefaultArtwork(Context context,boolean small) {  

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

  133.         opts.inPreferredConfig = Bitmap.Config.RGB_565;  

  134.         if(small){  //返回小图片  

  135.             return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.music5), null, opts);  

  136.         }  

  137.         return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.defaultalbum), null, opts);  

  138.     }  

  139.       

  140.       

  141.     /** 

  142.      * 从文件当中获取专辑封面位图 

  143.      * @param context 

  144.      * @param songid 

  145.      * @param albumid 

  146.      * @return 

  147.      */  

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

  149.         Bitmap bm = null;  

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

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

  152.         }  

  153.         try {  

  154.             BitmapFactory.Options options = new BitmapFactory.Options();  

  155.             FileDescriptor fd = null;  

  156.             if(albumid < 0){  

  157.                 Uri uri = Uri.parse("content://media/external/audio/media/"  

  158.                         + songid + "/albumart");  

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

  160.                 if(pfd != null) {  

  161.                     fd = pfd.getFileDescriptor();  

  162.                 }  

  163.             } else {  

  164.                 Uri uri = ContentUris.withAppendedId(albumArtUri, albumid);  

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

  166.                 if(pfd != null) {  

  167.                     fd = pfd.getFileDescriptor();  

  168.                 }  

  169.             }  

  170.             options.inSampleSize = 1;  

  171.             // 只进行大小判断  

  172.             options.inJustDecodeBounds = true;  

  173.             // 调用此方法得到options得到图片大小  

  174.             BitmapFactory.decodeFileDescriptor(fd, null, options);  

  175.             // 我们的目标是在800pixel的画面上显示  

  176.             // 所以需要调用computeSampleSize得到图片缩放的比例  

  177.             options.inSampleSize = 100;  

  178.             // 我们得到了缩放的比例,现在开始正式读入Bitmap数据  

  179.             options.inJustDecodeBounds = false;  

  180.             options.inDither = false;  

  181.             options.inPreferredConfig = Bitmap.Config.ARGB_8888;  

  182.               

  183.             //根据options参数,减少所需要的内存  

  184.             bm = BitmapFactory.decodeFileDescriptor(fd, null, options);  

  185.         } catch (FileNotFoundException e) {  

  186.             e.printStackTrace();  

  187.         }  

  188.         return bm;  

  189.     }  

  190.       

  191.     /** 

  192.      * 获取专辑封面位图对象 

  193.      * @param context 

  194.      * @param song_id 

  195.      * @param album_id 

  196.      * @param allowdefalut 

  197.      * @return 

  198.      */  

  199.     public static Bitmap getArtwork(Context context, long song_id, long album_id, boolean allowdefalut, boolean small){  

  200.         if(album_id < 0) {  

  201.             if(song_id < 0) {  

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

  203.                 if(bm != null) {  

  204.                     return bm;  

  205.                 }  

  206.             }  

  207.             if(allowdefalut) {  

  208.                 return getDefaultArtwork(context, small);  

  209.             }  

  210.             return null;  

  211.         }  

  212.         ContentResolver res = context.getContentResolver();  

  213.         Uri uri = ContentUris.withAppendedId(albumArtUri, album_id);  

  214.         if(uri != null) {  

  215.             InputStream in = null;  

  216.             try {  

  217.                 in = res.openInputStream(uri);  

  218.                 BitmapFactory.Options options = new BitmapFactory.Options();  

  219.                 //先制定原始大小  

  220.                 options.inSampleSize = 1;  

  221.                 //只进行大小判断  

  222.                 options.inJustDecodeBounds = true;  

  223.                 //调用此方法得到options得到图片的大小  

  224.                 BitmapFactory.decodeStream(in, null, options);  

  225.                 /** 我们的目标是在你N pixel的画面上显示。 所以需要调用computeSampleSize得到图片缩放的比例 **/  

  226.                 /** 这里的target为800是根据默认专辑图片大小决定的,800只是测试数字但是试验后发现完美的结合 **/  

  227.                 if(small){  

  228.                     options.inSampleSize = computeSampleSize(options, 40);  

  229.                 } else{  

  230.                     options.inSampleSize = computeSampleSize(options, 600);  

  231.                 }  

  232.                 // 我们得到了缩放比例,现在开始正式读入Bitmap数据  

  233.                 options.inJustDecodeBounds = false;  

  234.                 options.inDither = false;  

  235.                 options.inPreferredConfig = Bitmap.Config.ARGB_8888;  

  236.                 in = res.openInputStream(uri);  

  237.                 return BitmapFactory.decodeStream(in, null, options);  

  238.             } catch (FileNotFoundException e) {  

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

  240.                 if(bm != null) {  

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

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

  243.                         if(bm == null && allowdefalut) {  

  244.                             return getDefaultArtwork(context, small);  

  245.                         }  

  246.                     }  

  247.                 } else if(allowdefalut) {  

  248.                     bm = getDefaultArtwork(context, small);  

  249.                 }  

  250.                 return bm;  

  251.             } finally {  

  252.                 try {  

  253.                     if(in != null) {  

  254.                         in.close();  

  255.                     }  

  256.                 } catch (IOException e) {  

  257.                     e.printStackTrace();  

  258.                 }  

  259.             }  

  260.         }  

  261.         return null;  

  262.     }  

  263.   

  264.     /** 

  265.      * 对图片进行合适的缩放 

  266.      * @param options 

  267.      * @param target 

  268.      * @return 

  269.      */  

  270.     public static int computeSampleSize(Options options, int target) {  

  271.         int w = options.outWidth;  

  272.         int h = options.outHeight;  

  273.         int candidateW = w / target;  

  274.         int candidateH = h / target;  

  275.         int candidate = Math.max(candidateW, candidateH);  

  276.         if(candidate == 0) {  

  277.             return 1;  

  278.         }  

  279.         if(candidate > 1) {  

  280.             if((w > target) && (w / candidate) < target) {  

  281.                 candidate -= 1;  

  282.             }  

  283.         }  

  284.         if(candidate > 1) {  

  285.             if((h > target) && (h / candidate) < target) {  

  286.                 candidate -= 1;  

  287.             }  

  288.         }  

  289.         return candidate;  

  290.     }  

  291. }  

通过调用MediaUtil里的getArtwork()方法,就可以返回一个Bitmap对象

原文链接:http://www.apkbus.com/blog-85568-59751.html

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消