Esempio n. 1
0
  @SuppressLint("NewApi")
  @Override
  public CustomDrawable doDecode(byte[] bytes, DecodeInfo decodeInfo, UrlSizeKey key, int from) {
    long threadId = Thread.currentThread().getId();
    int viewWidth = key.mViewWidth;
    int viewHeight = key.mViewHeight;
    Options opts = decodeInfo.mBitmapOptions;
    opts.inJustDecodeBounds = true;
    opts.inSampleSize = 1;
    opts.outWidth = -1;
    opts.outHeight = -1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      opts.inBitmap = null;
    }
    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
    int bitmapWidth = opts.outWidth;
    int bitmapHeight = opts.outHeight;
    ImageLoaderLog.d(TAG, key.mUrl);
    ImageLoaderLog.d(TAG, threadId + " bitmap width:" + bitmapWidth + ",height:" + bitmapHeight);
    ImageLoaderLog.d(TAG, threadId + " view width:" + viewWidth + ",height:" + viewHeight);
    if (bitmapWidth <= 0 || bitmapHeight <= 0) {
      return null;
    }
    int sampleSize = getSampleSize(decodeInfo, bitmapWidth, bitmapHeight, viewWidth, viewHeight);
    bitmapWidth /= sampleSize;
    bitmapHeight /= sampleSize;
    ImageLoaderLog.d(TAG, threadId + " sampleSize:" + sampleSize);
    opts.inJustDecodeBounds = false;
    opts.inSampleSize = sampleSize;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      opts.inMutable = true;
      opts.inBitmap =
          ImageLoader.getInstance().mBitmapPool.get(bitmapWidth, bitmapHeight, false, false);
    }

    Bitmap bm = null;
    try {
      bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
      if (sampleSize > 1) {
        saveToDisk(bm, key);
      }
    } catch (Exception e) {
      ImageLoaderLog.d(TAG, threadId + " bitmap decode error");
      e.printStackTrace();
      return null;
    }

    if (bm != null) {
      ImageLoaderLog.d(
          TAG, threadId + " after bitmap width:" + bm.getWidth() + ",height:" + bm.getHeight());
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        BitmapLock.lockBitmap(bm);
      }
    }
    BitmapDrawable bd = BitmapDrawableFactory.createBitmapDrawable(bm);
    return bd;
  }
Esempio n. 2
0
 @Override
 public SizeDrawable getSize(byte[] bytes, DecodeInfo decodeInfo) {
   Options opts = decodeInfo.mBitmapOptions;
   opts.inJustDecodeBounds = true;
   opts.inSampleSize = 1;
   opts.outWidth = -1;
   opts.outHeight = -1;
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     opts.inBitmap = null;
   }
   BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
   int bitmapWidth = opts.outWidth;
   int bitmapHeight = opts.outHeight;
   return new SizeDrawable(bitmapWidth, bitmapHeight);
 }
Esempio n. 3
0
  // This is the same as the method above except the source data comes
  // from a file descriptor instead of a byte array.
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public static Bitmap decodeUsingPool(
      JobContext jc, FileDescriptor fileDescriptor, Options options) {
    if (options == null) options = new BitmapFactory.Options();
    if (options.inSampleSize < 1) options.inSampleSize = 1;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inBitmap =
        (options.inSampleSize == 1) ? findCachedBitmap(jc, fileDescriptor, options) : null;
    try {
      Bitmap bitmap = DecodeUtils.decode(jc, fileDescriptor, options);
      if (options.inBitmap != null && options.inBitmap != bitmap) {
        GalleryBitmapPool.getInstance().put(options.inBitmap);
        options.inBitmap = null;
      }
      return bitmap;
    } catch (IllegalArgumentException e) {
      if (options.inBitmap == null) throw e;

      Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap");
      GalleryBitmapPool.getInstance().put(options.inBitmap);
      options.inBitmap = null;
      return decode(jc, fileDescriptor, options);
    }
  }
Esempio n. 4
0
 @TargetApi(11)
 private void copyOptions11(Options srcOptions, Options destOptions) {
   destOptions.inBitmap = srcOptions.inBitmap;
   destOptions.inMutable = srcOptions.inMutable;
 }