Volley DiskBasedCache throws FileNotFoundException
Asked Answered
S

1

0

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;
    }
}
Sissified answered 4/10, 2014 at 13:54 Comment(1)
post your cache codeOxeyed
O
2

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.

Oxeyed answered 8/10, 2014 at 3:20 Comment(11)
Yes I guessed the 5MB limit was the culprit. How do I increase it?Sissified
private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024; from the class of DiskBasedCacheOxeyed
So should I extend the class and change the value?Sissified
No, just replace 5 by for example 10.Oxeyed
you should change it and you must use volley as a library project.Oxeyed
Cool, will do that. Thanks for the answer and suggestions!Sissified
Hey, I got around the FileNotFoundExceptions, but Volley still doesn't load all the bitmaps from cache. (It was same previously.) Current app cache is around 4.71MB. That's less than 5MB. Why would this happen? Here's a screenshot s3.amazonaws.com/pushbullet-uploads/…Sissified
have you increased cache size?Oxeyed
Yes, but I'm still using Volley as an AAR, with this code. gist.github.com/vibinr/6aac5ceda47c76463d95Sissified
What else do you think would go wrong? Please guide or give a hintSissified
sorry, I helped you, but I can not now!Oxeyed

© 2022 - 2024 — McMap. All rights reserved.