Is it possible to change the size of the cache Picasso uses for images?

社会主义新天地 提交于 2019-11-30 16:11:16

问题


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?


回答1:


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.




回答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();


来源:https://stackoverflow.com/questions/30705408/is-it-possible-to-change-the-size-of-the-cache-picasso-uses-for-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!