gpt4 book ai didi

android - 将 Square Picasso 的缓存大小限制为最大 60MB

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:19 25 4
gpt4 key购买 nike

我目前正在使用 Picasso 在我的应用程序中的多个回收器 View 中下载和缓存图像。到目前为止,Picasso 已经使用了大约 49MB 的缓存大小,我担心随着更多图像的使用,缓存大小会变得更高。

我正在使用默认的 Picasso.with(context) 对象。请回答以下问题:

1) 有没有办法限制 Picasso 缓存的大小。 MemoryPolicy 和 NetworkPolicy 设置为 NO_CACHE 不是一个选项。我需要缓存但达到一定级别(最大 60MB)

2) picasso 有没有办法像 Glide DiskCacheStrategy.RESULT 那样存储调整大小/裁剪后的图像

3) 如果选项是使用 OKHTTP,请指导我使用它来限制 Picasso 的缓存大小的好教程。 ( picasso 2.5.2)

4) 由于我使用的是 Picasso 的 Gradle 依赖项,我如何添加一个清晰的缓存函数,如下所示:

Clear Cache memory of Picasso

最佳答案

请试试这个,它对我来说似乎很管用:

我将其用作单例。只需将 60 放在 DISK/CACHE 大小参数所在的位置即可。

//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 && context != null) {
//Create disk cache folder if does not exist
File cache = new File(context.getApplicationContext().getCacheDir(), "picasso_cache");
if (!cache.exists())
cache.mkdirs();

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);
mOkHttpClient.setRetryOnConnectionFailure(true);
mOkHttpClient.setCache(diskCache);

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

}
}
return mInstance;
}

public static void updatePicassoInstance() {
mInstance = null;
}

public static void clearCache() {
if(lruCache != null) {
lruCache.clear();
}
try {
if(diskCache != null) {
diskCache.evictAll();
}
} catch (IOException e) {
e.printStackTrace();
}

lruCache = null;
diskCache = null;
}
}

关于android - 将 Square Picasso 的缓存大小限制为最大 60MB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com