2

I'm loading images from URLs (http://) with Picasso. Sometimes when i try to "preload" an image using Picasso's fetch() method, the image doesn't get cached. I guess it's because it's size is too big.

Read the answer for this question, but setCache() doesn't seem to be recognized for me, i don't even find it in the Picasso documentation.

Is there any way to change the cache size Picasso uses for bitmaps?

Community
  • 1
  • 1

2 Answers2

4

You can do:

int maxSize = MAX_CACHE_SIZE;
Picasso picasso = new Picasso.Builder(context)
                              .memoryCache(new LruCache(maxSize))
                              .build();

Picasso uses a Cache interface type to manage the cache. They provide the default implementation, LruCache, which has a constructor that accepts the max size in bytes as an argument.

Seems like the other answer uses the wrong function. It should be memoryCache, not setCache.

Tushar
  • 8,019
  • 31
  • 38
2

This example use OkHttp as http client for Picasso and setup max Disk cache size and also memory cache.

 // Size in bytes (10 MB)
 private static final long PICASSO_DISK_CACHE_SIZE = 1024 * 1024 * 10;

 // Use OkHttp as downloader
 Downloader downloader = new OkHttpDownloader(getApplicationContext(),
                        PICASSO_DISK_CACHE_SIZE);

  // Create memory cache
  Cache memoryCache = new LruCache(maxSize);

  mPicasso = new Picasso.Builder(getApplicationContext())
                        .downloader(downloader).memoryCache(memoryCache).build();
dasar
  • 5,321
  • 4
  • 24
  • 36
  • hay @dasar i'am using picasso to load images from disc as follow picasso.with(context).load(file).into(imageview); ...... how can i set memory cash this way? thanx in advance – Error Nov 14 '15 at 18:24
  • 1
    @Error you need to build Picasso instance using builder and call Picasso.setSingletonInstance(picasso) than you can continue using it as you do now. – dasar Nov 15 '15 at 06:19
  • yes it works but i must create a singleton in class that extend application . – Error Nov 17 '15 at 10:30
  • @Error Picasso itself has static method setSingletonInstance() exactly for this purpose. You just need to build instance and set it as singleton using this method, piccaso will do the rest. You will use Picasso as usual by calling Picasso.with(ctx) but it will use singleton instance under the hood. And yes, probably Application class it good place to build Picasso instance or you can do it in other place, but just do it before you will need it. – dasar Nov 17 '15 at 10:34
  • follow the above way, + set the mPicasso object to the Picasso's singleton method. So, the same object will be given on Picasso.get(). – Charan Jan 30 '19 at 03:15
  • Does is take the set memory space during creation or just only when the first image is stored there (lazily)? – K.Os Jul 25 '19 at 08:55