diff --git a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java index 6a8e0bba6c..0540e469e3 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java @@ -128,7 +128,7 @@ public Bitmap get(int width, int height, Bitmap.Config config) { // contents individually, so we do so here. See issue #131. result.eraseColor(Color.TRANSPARENT); } else { - result = Bitmap.createBitmap(width, height, config); + result = createBitmap(width, height, config); } return result; @@ -139,11 +139,16 @@ public Bitmap get(int width, int height, Bitmap.Config config) { public Bitmap getDirty(int width, int height, Bitmap.Config config) { Bitmap result = getDirtyOrNull(width, height, config); if (result == null) { - result = Bitmap.createBitmap(width, height, config); + result = createBitmap(width, height, config); } return result; } + @NonNull + private static Bitmap createBitmap(int width, int height, @Nullable Bitmap.Config config) { + return Bitmap.createBitmap(width, height, config != null ? config : DEFAULT_CONFIG); + } + @TargetApi(Build.VERSION_CODES.O) private static void assertNotHardwareConfig(Bitmap.Config config) { // Avoid short circuiting on sdk int since it breaks on some versions of Android. @@ -159,7 +164,8 @@ private static void assertNotHardwareConfig(Bitmap.Config config) { } @Nullable - private synchronized Bitmap getDirtyOrNull(int width, int height, Bitmap.Config config) { + private synchronized Bitmap getDirtyOrNull( + int width, int height, @Nullable Bitmap.Config config) { assertNotHardwareConfig(config); // Config will be null for non public config types, which can lead to transformations naively // passing in null as the requested config here. See issue #194. diff --git a/library/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java b/library/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java index 6ccc7a1682..0b452c214e 100644 --- a/library/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java +++ b/library/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java @@ -138,6 +138,18 @@ public void testPassesArgb8888ToStrategyAsConfigForRequestsWithNullConfigsOnGetD assertEquals(expected, result); } + @Test + public void get_withNullConfig_andEmptyPool_returnsNewArgb8888Bitmap() { + Bitmap result = pool.get(100, 100, /*config=*/ null); + assertThat(result.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888); + } + + @Test + public void getDirty_withNullConfig_andEmptyPool_returnsNewArgb8888Bitmap() { + Bitmap result = pool.getDirty(100, 100, /*config=*/ null); + assertThat(result.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888); + } + private void testTrimMemory(int fillSize, int trimLevel, int expectedSize) { MockStrategy strategy = new MockStrategy(); LruBitmapPool pool = new LruBitmapPool(MAX_SIZE, strategy, ALLOWED_CONFIGS); @@ -240,7 +252,7 @@ public void put(Bitmap bitmap) { @Override public Bitmap get(int width, int height, Bitmap.Config config) { - return bitmaps.removeLast(); + return bitmaps.isEmpty() ? null : bitmaps.removeLast(); } @Override