提交 e7812093 编写于 作者: S Sam Judd

Workaround for framework NPE on KitKat.

Fixes #148
上级 a9d87fb5
......@@ -3,6 +3,7 @@ package com.bumptech.glide.load.engine.bitmap_recycle;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Build;
import com.bumptech.glide.util.Util;
import java.util.TreeMap;
......@@ -19,7 +20,8 @@ class SizeStrategy implements LruPoolStrategy {
@Override
public void put(Bitmap bitmap) {
final Key key = keyPool.get(bitmap.getAllocationByteCount());
int size = Util.getSize(bitmap);
final Key key = keyPool.get(size);
groupedMap.put(key, bitmap);
......@@ -52,7 +54,7 @@ class SizeStrategy implements LruPoolStrategy {
public Bitmap removeLast() {
Bitmap removed = groupedMap.removeLast();
if (removed != null) {
final int removedSize = removed.getAllocationByteCount();
final int removedSize = Util.getSize(removed);
decrementBitmapOfSize(removedSize);
}
return removed;
......@@ -79,7 +81,7 @@ class SizeStrategy implements LruPoolStrategy {
@Override
public int getSize(Bitmap bitmap) {
return bitmap.getAllocationByteCount();
return Util.getSize(bitmap);
}
@Override
......@@ -97,7 +99,8 @@ class SizeStrategy implements LruPoolStrategy {
}
private static String getBitmapString(Bitmap bitmap) {
return getBitmapString(bitmap.getAllocationByteCount());
int size = Util.getSize(bitmap);
return getBitmapString(size);
}
private static String getBitmapString(int size) {
......
......@@ -40,15 +40,19 @@ public class Util {
}
/**
* Returns the in memory size of the given {@link Bitmap}.
* Returns the in memory size of the given {@link Bitmap} in bytes.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return bitmap.getHeight() * bitmap.getRowBytes();
} else {
return bitmap.getAllocationByteCount();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException e) {
// Do nothing.
}
}
return bitmap.getHeight() * bitmap.getRowBytes();
}
public static void assertMainThread() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册