I'm using Picasso to display images. Is it possible to force clear the cache when I need? I googled it but I cannot find any solution.
Thank you.
I'm using Picasso to display images. Is it possible to force clear the cache when I need? I googled it but I cannot find any solution.
Thank you.
 
    
    Based on this other question and its self answer: Invalidate cache in Picasso
There is an easier way to do it without forking the library. Add this class to the com.squareup.picasso package.
package com.squareup.picasso;
public class PicassoTools {
    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}
Because cache has package visibility, this util class can clear the cache for you. You just have to call it:
PicassoTools.clearCache(Picasso.with(context));
Use this code
import java.io.File;
import android.content.Context;
import com.androlizer.yify.torrent.R;
public class ClearCache {
    public static void trimCache(Context context) {
        try {
            File dir = context.getCacheDir();
            if (dir != null && dir.isDirectory()) {
                if (deleteDir(dir)) {
                    CreateSuperToast.createInfoToast(context, context.getString(R.string.cache_cleared));
                }else{
                    CreateSuperToast.createInfoToast(context, context.getString(R.string.cache_not_cleared));
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // The directory is now empty so delete it
        return dir.delete();
    }
}
