凌空DiskBasedCache抛出FileNotFoundException异常 [英] Volley DiskBasedCache throws FileNotFoundException

查看:337
本文介绍了凌空DiskBasedCache抛出FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用<一个href=\"https://github.com/rdrobinson3/VolleyImageCacheExample/blob/master/CaptechBuzz/src/com/captechconsulting/captechbuzz/model/images/BitmapLruImageCache.java\"相对=nofollow> BitmapLRUCache由罗宾逊的三分球的在我的Andr​​oid应用程序的图像缓存。这是LRU缓存排球的实现,因为它本身并不能提供任何的图像缓存。

虽然它并使用<一个href=\"https://android.googlesource.com/platform/frameworks/volley/+/d62a616ebca5bfa4f9ec5517208e13f2d501b69a/src/com/android/volley/toolbox/DiskBasedCache.java\"相对=nofollow> DiskBasedCache 缓存HTTP请求。现在来这个问题,我得到FileNotFoundExceptions时反复DiskBasedCache试图获取或删除缓存条目。

下面的示例日志。

  23833凌空D [47291] DiskBasedCache.remove:无法删除缓存项key=http://a2.mzstatic.com/us/r30/Music1/v4/69/66/0b/69660b50-7771-a43a-919f-26d8b6ae37aa/UMG_cvrart_00602537957941_01_RGB72_1500x1500_14UMGIM31675.400x400-75.jpg,文件名= 1509125231-2004731303
 23833凌空D [47291] DiskBasedCache.get:/data/data/com.vibin.billy.debug/cache/volley/6408634861932551223:java.io.FileNotFoundException:/data/data/com.vibin.billy.debug/cache/凌空/ 6408634861932551223:打开失败:ENOENT(没有这样的文件或目录)
 23833凌空D [47291] DiskBasedCache.remove:无法删除缓存项key=http://a2.mzstatic.com/us/r30/Music4/v4/99/f7/ac/99f7ac13-0dd6-8841-96e0-2a1c18041d84/UMG_cvrart_00602537854097_01_RGB72_1800x1800_14UMGIM03851.400x400-75.jpg,文件名= 6408634861932551223

为什么DiskBasedCache处理,当我用初始化BitmapLRUcache(见下文)?

的ImageLoader的图像缓存

  ImageLoader的ImageLoader的=新ImageLoader的(Volley.newRequestQueue(本),新BitmapLruCache());

下面是code我使用缓存。

 包com.vibin.billy;进口android.graphics.Bitmap;
进口android.support.v4.util.LruCache;
进口android.util.Log;进口com.android.volley.toolbox.DiskBasedCache;
进口com.android.volley.toolbox.ImageLoader;/ **
 *基本LRU内存缓存。
 *
 * @author罗宾逊三分球
 * /
公共类BitmapLruCache
        扩展LruCache&LT;弦乐,位图&GT;
        实现ImageLoader.ImageCache {    私有静态最后弦乐TAG = BitmapLruCache.class.getSimpleName();    公共BitmapLruCache(){
        这个(getDefaultLruCacheSize());
    }    公共BitmapLruCache(INT sizeInKiloBytes){
        超(sizeInKiloBytes);
    }
    @覆盖
    保护INT整型尺寸(字符串键,位值){
        返回value.getRowBytes()* value.getHeight()/ 1024;
    }    @覆盖
    公共位图getBitmap(字符串URL){
        //Log.d(TAG,抢+网址);
        得到的回报(URL);
    }    @覆盖
    公共无效putBitmap(字符串URL,位图位图){
         //Log.d(TAG,放+网址);
         把(URL,位图);
    }    公共静态INT getDefaultLruCacheSize(){
        最终诠释maxMemory =
                (中间体)(调用Runtime.getRuntime()maxMemory()/ 1024);
        最终诠释的cachesize = maxMemory / 8;        Log.d(TAG的cachesize是+ CACHESIZE);        Log.d(TAG,CACHESIZE +是缓存大小);        返回CACHESIZE;
    }
}


解决方案

  

为什么DiskBasedCache处理,当我初始化图像缓存
  ImageLoader的与BitmapLRUcache(见下文)?


图像缓存凌空使用2级缓存机制,这意味着一个层面是在你的 RAM BitmapLRUcache 和另一一个是你的磁盘 DiskBasedCache 。为什么?因为从磁盘读取和写入图像需要更长的时间比只是简单的阅读和写作部分字符串并给出了表现不佳。因此在排球第一次当你请求下载你的图片排球首先着眼于高速缓存级别之一,如果您图像是不存在排球着眼于高速缓存级别2,如果你的图片是不是有排球发送您的下载请求服务器。


  

我得到FileNotFoundExceptions时反复DiskBasedCache尝试
  获取或删除缓存条目。


因为你的 DiskBasedCache 尺寸限制(默认为5MB),也有 LRU ,这意味着如果排球要存储在图像的 DiskBasedCache ,它剂量不会有任何空间,它会删除一些它持有并于最近剂量不是引用旧的条目。(LRU =最近最少使用)所以remove函数被调用。

I'm using BitmapLRUCache by Trey Robinson for image caching in my Android app. It's an implementation of LRU cache for Volley as it doesn't provide any image caching by itself.

Though it does use DiskBasedCache for caching HTTP requests. Now coming to the problem, I get FileNotFoundExceptions repeatedly when DiskBasedCache tries to get or remove cache entries.

Sample log below.

 23833                 Volley  D  [47291] DiskBasedCache.remove: Could not delete cache entry for key=http://a2.mzstatic.com/us/r30/Music1/v4/69/66/0b/69660b50-7771-a43a-919f-26d8b6ae37aa/UMG_cvrart_00602537957941_01_RGB72_1500x1500_14UMGIM31675.400x400-75.jpg, filename=1509125231-2004731303
 23833                 Volley  D  [47291] DiskBasedCache.get: /data/data/com.vibin.billy.debug/cache/volley/6408634861932551223: java.io.FileNotFoundException: /data/data/com.vibin.billy.debug/cache/volley/6408634861932551223: open failed: ENOENT (No such file or directory)
 23833                 Volley  D  [47291] DiskBasedCache.remove: Could not delete cache entry for key=http://a2.mzstatic.com/us/r30/Music4/v4/99/f7/ac/99f7ac13-0dd6-8841-96e0-2a1c18041d84/UMG_cvrart_00602537854097_01_RGB72_1800x1800_14UMGIM03851.400x400-75.jpg, filename=6408634861932551223

Why is DiskBasedCache handling image caching when I'm initializing the ImageLoader with BitmapLRUcache (see below)?

ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(this), new BitmapLruCache());

Below is the code I'm using for caching.

package com.vibin.billy;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;

import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.ImageLoader;

/**
 * Basic LRU Memory cache.
 *
 * @author Trey Robinson
 */
public class BitmapLruCache
        extends LruCache<String, Bitmap>
        implements ImageLoader.ImageCache {

    private static final String TAG = BitmapLruCache.class.getSimpleName();

    public BitmapLruCache() {
        this(getDefaultLruCacheSize());
    }

    public BitmapLruCache(int sizeInKiloBytes) {
        super(sizeInKiloBytes);
    }


    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    @Override
    public Bitmap getBitmap(String url) {
        //Log.d(TAG, "Grab "+url);
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
         //Log.d(TAG, "Put "+url);
         put(url, bitmap);
    }

    public static int getDefaultLruCacheSize() {
        final int maxMemory =
                (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;

        Log.d(TAG, "cachesize is " + cacheSize);

        Log.d(TAG,cacheSize+" is cache Size");

        return cacheSize;
    }
}

解决方案

Why is DiskBasedCache handling image caching when I'm initializing the ImageLoader with BitmapLRUcache (see below)?

for image caching volley use 2 level cache mechanism, that means one level is in your RAM BitmapLRUcache and another one is on your Disk DiskBasedCache. why? because of reading and writing images from disk takes longer time than just simply reading and writing some Strings and gives poor performance. so at the first time when you request Volley to download your images Volley first looks at the cache level one, if your images are not there Volley looks at cache level 2 and if your images are not there Volley sends your download request to the server.

I get FileNotFoundExceptions repeatedly when DiskBasedCache tries to get or remove cache entries.

because your DiskBasedCache size is limited(by default is 5MB) and also it is LRU, which means if Volley wants to store an image on the DiskBasedCache and it dose not have any space, it is going to delete some of the old entries that it holds and dose not references recently.(LRU=Least Recently Used) So the remove function is called.

这篇关于凌空DiskBasedCache抛出FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆