Kushan Kushan - 3 months ago 35
Android Question

Limiting Square Picasso's cache size to 60MB max

I am currently using Picasso to download and cache images in my app inside multiple recycler views. So far Picasso has used around 49MB cache size and i am worried that as more images come into play, this will become much higher.

I am using the default

object. Please answer the following:

1) Is there a way to restrict the Size of Picasso's cache. MemoryPolicy and NetworkPolicy set to NO_CACHE isn't an option. I need caching but upto a certain level (60MB max)

2) Is there a way in picasso to store Resized/cropped images like in Glide DiskCacheStrategy.RESULT

3) If the option is to use OKHTTP, please guide me to a good tutorial for using it to limit Picasso's cache size. (Picasso 2.5.2)

4) Since i am using a Gradle dependency of Picasso, how can i add a clear Cache function as shown here:

Clear Cache memory of Picasso

Gil Gil

Please try this one, it does seem to work great for me:

I use it as a Singleton. Just put 60 where DISK/CACHE size parameters are.

//Singleton Class for Picasso Downloading, Caching and Displaying Images Library
public class PicassoSingleton {

    private static Picasso mInstance;
    private static long mDiskCacheSize = CommonConsts.DISK_CACHE_SIZE *1024*1024; //Disk Cache
    private static int mMemoryCacheSize = CommonConsts.MEMORY_CACHE_SIZE *1024*1024; //Memory Cache
    private static OkHttpClient mOkHttpClient; //OK Http Client for downloading
    private static Cache diskCache;
    private static LruCache lruCache;

public static Picasso getSharedInstance(Context context)
        if(mInstance == null) {
            if (context != null) {
                //Create disk cache folder if does not exist
                File cache = new File(context.getApplicationContext().getCacheDir(), "picasso_cache");
                if (!cache.exists())

                diskCache = new Cache(cache, mDiskCacheSize);
                lruCache = new LruCache(mMemoryCacheSize);
                //Create OK Http Client with retry enabled, timeout and disk cache
                mOkHttpClient = new OkHttpClient();
                mOkHttpClient.setConnectTimeout(CommonConsts.SECONDS_TO_OK_HTTP_TIME_OUT, TimeUnit.SECONDS);

                //For better performence in Memory use set memoryCache(Cache.NONE) in this builder (If needed)
                mInstance = new Picasso.Builder(context).memoryCache(lruCache)/*.executor(Executors.newSingleThreadExecutor())*/.downloader(new OkHttpDownloader(mOkHttpClient)).indicatorsEnabled(CommonConsts.SHOW_PICASSO_INDICATORS).build();

        return mInstance;

public static void updatePicassoInstance()
    mInstance = null;

public static void clearCache()
    try {
    } catch (IOException e) {

    lruCache = null;
    diskCache = null;