使用 DiskLruCache 实现图片 Lru 缓存
Implementing Image Lru Caching with DiskLruCache
我正在尝试使用 jake wharton DiskLruCache 库实现基于磁盘的图像 lru 缓存。 link to library. I am using a code snippet from here.
我遇到问题的方法是这个
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
确保导入正确class:
import com.jakewharton.disklrucache.DiskLruCache
我正在尝试使用 jake wharton DiskLruCache 库实现基于磁盘的图像 lru 缓存。 link to library. I am using a code snippet from here.
我遇到问题的方法是这个
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
确保导入正确class:
import com.jakewharton.disklrucache.DiskLruCache