此代码可用于 Android 7 ,但不适用于 Android 8 引发IllegalStateException
Map<String, Bitmap> snapshot = mMemoryCache.snapshot();
for (Bitmap bitmap : snapshot.values()) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
}
mMemoryCache.evictAll();
这是LruCache.java中的错误
public void trimToSize(int maxSize) {
while (true) {
K key;
V value;
synchronized (this) {
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize) {
break;
}
Map.Entry<K, V> toEvict = map.eldest();
if (toEvict == null) {
break;
}
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);
size -= safeSizeOf(key, value);
evictionCount++;
}
entryRemoved(true, key, value, null);
}