6

I'm using fedor's lazy loading list implementation in my test application where I can clear the cache with a single button click. How can I get the cache size of the loaded images in the listview and clear the cache programmatically?

Here is the code for saving the cached images:

public ImageLoader(Context context){
    //Make the background thead low priority. This way it will not affect the UI performance.
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
    mAssetManager = context.getAssets();

    //Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
    else
        cacheDir = context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();
}

EDIT:

So basically I added this piece of code in clearCache(), method, but I still cannot see the images start loading again when I'm scrolling.

public void clearCache() {
    //clear memory cache

    long size=0;
    cache.clear();

    //clear SD cache
    File[] files = cacheDir.listFiles();
    for (File f:files) {
        size = size+f.length();
        if(size >= 200)
            f.delete();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Android-Droid
  • 14,365
  • 41
  • 114
  • 185

4 Answers4

7

To find the size of the cache directory use the codebelow.

public void clearCache() {
    //clear memory cache

    long size = 0;
    cache.clear();

    //clear SD cache
    File[] files = cacheDir.listFiles();
    for (File f:files) {
        size = size+f.length();
        f.delete();
    }
}

This will return the number of bytes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ilango j
  • 5,967
  • 2
  • 28
  • 25
  • I just edit my question with the code I'm using now,but still can't see the images loading after scrolling down. – Android-Droid Aug 12 '11 at 07:22
  • where you are calling clear cache? and why you are doing this code if(size>=200) f.delete(); – ilango j Aug 12 '11 at 07:27
  • I'm calling this in my main activity like this : adapter.imageLoader.clearCache(); adapter.notifyDataSetChanged(); . And I put the IF,because I want it to delete the cache when it's size reach maybe 200kb.Is that right I'm doing? – Android-Droid Aug 12 '11 at 07:30
  • Ok,and how I can call that piece of code to clear the cache when it's like 200kb? – Android-Droid Aug 12 '11 at 07:37
  • use the above code. i think tat may solve your problem. now i have edited that code? – ilango j Aug 12 '11 at 08:21
  • call that clearCache() method in Adapter class constructor. before calling getview() method the cache will be cleared. – ilango j Aug 12 '11 at 08:31
5

This has been more accurate to me:

private void initializeCache() {
    long size = 0;
    size += getDirSize(this.getCacheDir());
    size += getDirSize(this.getExternalCacheDir());
}

public long getDirSize(File dir){
    long size = 0;
    for (File file : dir.listFiles()) {
        if (file != null && file.isDirectory()) {
            size += getDirSize(file);
        } else if (file != null && file.isFile()) {
            size += file.length();
        }
    }
    return size;
}
fede1608
  • 2,808
  • 1
  • 16
  • 17
3

In Kotlin, you can use:

context.cacheDir.walkBottomUp().fold(0L, { acc, file -> acc + file.length() })

or define it as an extension function

fun File.calculateSizeRecursively(): Long {
    return walkBottomUp().fold(0L, { acc, file -> acc + file.length() })
}


// usage
val size = context.cacheDir.calculateSizeRecursively()

beigirad
  • 4,986
  • 2
  • 29
  • 52
1

...and to clear cache, just delete the directory and recreate an empty one.

Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69