Exemplo n.º 1
0
  private CloseableReference<Bitmap> decodeStaticImageFromStream(
      InputStream inputStream, BitmapFactory.Options options) {
    Preconditions.checkNotNull(inputStream);
    int sizeInBytes =
        BitmapUtil.getSizeInByteForBitmap(
            options.outWidth, options.outHeight, options.inPreferredConfig);
    final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes);
    if (bitmapToReuse == null) {
      throw new NullPointerException("BitmapPool.get returned null");
    }
    options.inBitmap = bitmapToReuse;

    Bitmap decodedBitmap;
    ByteBuffer byteBuffer = mDecodeBuffers.acquire();
    if (byteBuffer == null) {
      byteBuffer = ByteBuffer.allocate(DECODE_BUFFER_SIZE);
    }
    try {
      options.inTempStorage = byteBuffer.array();
      decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
    } catch (RuntimeException re) {
      mBitmapPool.release(bitmapToReuse);
      throw re;
    } finally {
      mDecodeBuffers.release(byteBuffer);
    }

    if (bitmapToReuse != decodedBitmap) {
      mBitmapPool.release(bitmapToReuse);
      decodedBitmap.recycle();
      throw new IllegalStateException();
    }

    return CloseableReference.of(decodedBitmap, mBitmapPool);
  }
Exemplo n.º 2
0
 public ArtDecoder(BitmapPool bitmapPool, int maxNumThreads) {
   mBitmapPool = bitmapPool;
   mDecodeBuffers = new SynchronizedPool<>(maxNumThreads);
   for (int i = 0; i < maxNumThreads; i++) {
     mDecodeBuffers.release(ByteBuffer.allocate(DECODE_BUFFER_SIZE));
   }
 }