【问题标题】:Implementing Image Lru Caching with DiskLruCache使用 DiskLruCache 实现图像 Lru 缓存
【发布时间】:2023-03-26 22:15:01
【问题描述】:

我正在尝试使用 jake wharton DiskLruCache 库来实现基于磁盘的图像 lru 缓存。 link to library。我正在使用来自here 的代码 sn-p。

我遇到问题的方法是这个

private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
        throws IOException, FileNotFoundException {
    OutputStream out = null;
    try {
        out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE);
        return bitmap.compress(mCompressFormat, mCompressQuality, out);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

其中 editor.newOutputStream(0) 不存在,而这个

public Bitmap getBitmap(String key) {

    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = null;
    try {

        snapshot = mDiskCache.get(key);
        if (snapshot == null) {
            return null;
        }
        final InputStream in = snapshot.getInputStream(0);
        if (in != null) {
            final BufferedInputStream buffIn =
                    new BufferedInputStream(in, Utils.IO_BUFFER_SIZE);
            bitmap = BitmapFactory.decodeStream(buffIn);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (snapshot != null) {
            snapshot.close();
        }
    }

        Log.d("cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);

    return bitmap;

}

其中 snapshot.getInputStream(0) 也不存在。

我做错了什么?我已将 jar 与库一起放置,一切都很好,这些方法是否已从 DiskLruCache 库中删除?现在还有其他方法可以做到这一点吗?我找不到任何示例或教程。

库版本是最新的disklrucache-2.0.2

【问题讨论】:

  • 你有写入存储的权限吗?
  • 你的意思是方法无法解析?
  • 是的,但这与缺少方法有什么关系?
  • 我一开始不明白这个问题。
  • 您是如何发现这些方法不存在的:来自编辑器突出显示还是在编译时?

标签: android bitmap android-lru-cache


【解决方案1】:

确保您已导入正确的类:

import com.jakewharton.disklrucache.DiskLruCache

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2021-03-05
    • 2010-11-03
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多