How to get cache size in Android

I use Fedora lazy to implement a download list in my test application, where I can clear the cache with the click of a button. How can I get the cache size of the downloaded images in the list and clear the cache programmatically?

Here is the code for saving 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 to the clearCache () method, but I still don't see how the images start to load again when I scroll.

 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(); } } 
+1
android caching get size
Aug 12 2018-11-11T00:
source share
3 answers

To find the size of the cache directory, use the code below.

 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.

+3
Aug 12 2018-11-11T00:
source share

This was more accurate for 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; } 
+1
Feb 18 '16 at 17:12
source share

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

0
Aug 12 '11 at 7:17
source share



All Articles