我正在尝试使用 jake wharton DiskLruCache 库实现基于磁盘的图像 lru 缓存。链接到图书馆。我正在使用这里的代码片段。
我遇到问题的方法是这个
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