获取安卓音乐专辑图片

public class ArtworkUtils {

      private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
      private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();

      public static Bitmap getArtwork(Context context, String title, long song_id, long album_id,
      boolean allowdefault) {
      if (album_id < 0) {
      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);
      Bitmap bmp = BitmapFactory.decodeStream(in, null, sBitmapOptions);
      if (bmp == null) {
      bmp = getDefaultArtwork(context);
      }
      return bmp;
      } catch (FileNotFoundException ex) {
      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 e) {
      e.printStackTrace();
      }
      }
      }
      return null;
      }

      private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
      Bitmap bm = 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) {

      }
      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.music_icon), null,
      opts);
      }
将上述代码考入一个类,修改一下类名,直接调用即可,简单方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值