【问题标题】:android writing images to cache not returning null on decode fileandroid将图像写入缓存而不是在解码文件上返回null
【发布时间】: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

感谢您的帮助。

【问题讨论】:

    标签: android caching imageview


    【解决方案1】:

    提示:解码前检查文件大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      相关资源
      最近更新 更多