IllegalStateException-LruCache-evictAll ---> trimToSize

时间:2018-07-15 04:01:30

标签: java android

此代码可用于 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);
    }
  • 如果我回收了位图,是否需要调用evictAll?
  • 如果是,我如何解决此问题?
  • 还有其他指针/建议吗?

0 个答案:

没有答案