List of usage examples for android.graphics Bitmap reconfigure
public void reconfigure(int width, int height, Config config)
Modifies the bitmap to have a specified width, height, and Config , without affecting the underlying allocation backing the bitmap.
From source file:com.facebook.imagepipeline.platform.DefaultDecoder.java
/** * Create a bitmap from an input stream. * * @param inputStream the InputStream//w w w. ja va2 s .c om * @param options the {@link android.graphics.BitmapFactory.Options} used to decode the stream * @param regionToDecode optional image region to decode or null to decode the whole image * @param transformToSRGB whether to allow color space transformation to sRGB at load time * @return the bitmap */ private CloseableReference<Bitmap> decodeFromStream(InputStream inputStream, BitmapFactory.Options options, @Nullable Rect regionToDecode, final boolean transformToSRGB) { Preconditions.checkNotNull(inputStream); int targetWidth = options.outWidth; int targetHeight = options.outHeight; if (regionToDecode != null) { targetWidth = regionToDecode.width() / options.inSampleSize; targetHeight = regionToDecode.height() / options.inSampleSize; } int sizeInBytes = getBitmapSize(targetWidth, targetHeight, options); final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes); if (bitmapToReuse == null) { throw new NullPointerException("BitmapPool.get returned null"); } options.inBitmap = bitmapToReuse; // Performs transformation at load time to sRGB. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && transformToSRGB) { options.inPreferredColorSpace = ColorSpace.get(ColorSpace.Named.SRGB); } Bitmap decodedBitmap = null; ByteBuffer byteBuffer = mDecodeBuffers.acquire(); if (byteBuffer == null) { byteBuffer = ByteBuffer.allocate(DECODE_BUFFER_SIZE); } try { options.inTempStorage = byteBuffer.array(); if (regionToDecode != null) { BitmapRegionDecoder bitmapRegionDecoder = null; try { bitmapToReuse.reconfigure(targetWidth, targetHeight, options.inPreferredConfig); bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, true); decodedBitmap = bitmapRegionDecoder.decodeRegion(regionToDecode, options); } catch (IOException e) { FLog.e(TAG, "Could not decode region %s, decoding full bitmap instead.", regionToDecode); } finally { if (bitmapRegionDecoder != null) { bitmapRegionDecoder.recycle(); } } } if (decodedBitmap == null) { decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options); } } catch (IllegalArgumentException e) { mBitmapPool.release(bitmapToReuse); // This is thrown if the Bitmap options are invalid, so let's just try to decode the bitmap // as-is, which might be inefficient - but it works. try { // We need to reset the stream first inputStream.reset(); Bitmap naiveDecodedBitmap = BitmapFactory.decodeStream(inputStream); if (naiveDecodedBitmap == null) { throw e; } return CloseableReference.of(naiveDecodedBitmap, SimpleBitmapReleaser.getInstance()); } catch (IOException re) { // We throw the original exception instead since it's the one causing this workaround in the // first place. throw e; } } 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); }