【发布时间】:2012-03-31 20:08:31
【问题描述】:
我有以下方法将文件写入缓存:
private Bitmap getBitmap(String url) {
String filename = String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
// Is the bitmap in our cache?
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
if(bitmap != null) return bitmap;
// Nope, have to download it
try {
bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
// save bitmap to cache for later
writeFile(bitmap, f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
问题在于: if(bitmap != null) 返回位图;
此条件永远不会返回 null,因此我的缓存图像永远不会返回。如何查看文件的解码实际上是否失败?
我知道图像写入正确,因为我得到以下文件位置的日志输出:
/data/data/com.example/cache/1562036938
感谢您的帮助。
【问题讨论】: