diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 1677b1ff20..63b0f12c7f 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -351,7 +351,7 @@ private static void throwIncorrectGlideModule(Exception e) { new ResourceLoader.FileDescriptorFactory(resources); ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory = new ResourceLoader.AssetFileDescriptorFactory(resources); - BitmapEncoder bitmapEncoder = new BitmapEncoder(); + BitmapEncoder bitmapEncoder = new BitmapEncoder(arrayPool); BitmapBytesTranscoder bitmapBytesTranscoder = new BitmapBytesTranscoder(); GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder(); diff --git a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoder.java b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoder.java index 3c3e652754..8de58d9a30 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoder.java @@ -2,13 +2,16 @@ import android.graphics.Bitmap; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.os.TraceCompat; import android.util.Log; import com.bumptech.glide.load.EncodeStrategy; import com.bumptech.glide.load.Option; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.ResourceEncoder; +import com.bumptech.glide.load.data.BufferedOutputStream; import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; import com.bumptech.glide.util.LogTime; import com.bumptech.glide.util.Util; import java.io.File; @@ -49,6 +52,20 @@ public class BitmapEncoder implements ResourceEncoder { "com.bumptech.glide.load.resource.bitmap.BitmapEncoder.CompressionFormat"); private static final String TAG = "BitmapEncoder"; + @Nullable + private final ArrayPool arrayPool; + + public BitmapEncoder(@NonNull ArrayPool arrayPool) { + this.arrayPool = arrayPool; + } + + /** + * @deprecated Use {@link #BitmapEncoder(ArrayPool)} instead. + */ + @Deprecated + public BitmapEncoder() { + arrayPool = null; + } @Override public boolean encode(@NonNull Resource resource, @NonNull File file, @@ -65,6 +82,9 @@ public boolean encode(@NonNull Resource resource, @NonNull File file, OutputStream os = null; try { os = new FileOutputStream(file); + if (arrayPool != null) { + os = new BufferedOutputStream(os, arrayPool); + } bitmap.compress(format, quality, os); os.close(); success = true; diff --git a/library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoderTest.java b/library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoderTest.java index 9db7c49946..68d4a56c34 100644 --- a/library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoderTest.java +++ b/library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapEncoderTest.java @@ -10,6 +10,8 @@ import com.bumptech.glide.load.EncodeStrategy; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; +import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool; import com.bumptech.glide.util.ByteBufferUtil; import java.io.File; import java.io.IOException; @@ -86,13 +88,13 @@ public void testEncoderEncodesPngWithNullFormatAndBitmapWithAlpha() throws IOExc @Test public void testReturnsTrueFromWrite() { - BitmapEncoder encoder = new BitmapEncoder(); + BitmapEncoder encoder = new BitmapEncoder(harness.arrayPool); assertTrue(encoder.encode(harness.resource, harness.file, harness.options)); } @Test public void testEncodeStrategy_alwaysReturnsTransformed() { - BitmapEncoder encoder = new BitmapEncoder(); + BitmapEncoder encoder = new BitmapEncoder(harness.arrayPool); assertEquals(EncodeStrategy.TRANSFORMED, encoder.getEncodeStrategy(harness.options)); } @@ -105,6 +107,7 @@ private static class EncoderHarness { final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); final Options options = new Options(); final File file = new File(RuntimeEnvironment.application.getCacheDir(), "test"); + final ArrayPool arrayPool = new LruArrayPool(); EncoderHarness() { when(resource.get()).thenReturn(bitmap); @@ -119,7 +122,7 @@ void setFormat(Bitmap.CompressFormat format) { } String encode() throws IOException { - BitmapEncoder encoder = new BitmapEncoder(); + BitmapEncoder encoder = new BitmapEncoder(arrayPool); encoder.encode(resource, file, options); byte[] data = ByteBufferUtil.toBytes(ByteBufferUtil.fromFile(file)); return new String(data, "UTF-8");