我正在使用毕加索为我的应用加载图片。没有特别大,但它只是缓存到内存所以我在图像重页面上遇到内存不足错误。我需要在毕加索或模拟器中手动设置以启用磁盘缓存吗?
答案 0 :(得分:2)
您是否在Picasso中提供了自定义下载程序?你应该确定一些事项:
以下是将图像写入SD卡上的缓存目录的示例实现:
// Obtain the external cache directory
File cacheDir = context.getExternalCacheDir();
if (cacheDir == null) {
// Fall back to using the internal cache directory
cacheDir = context().getCacheDir();
}
// Create a response cache using the cache directory and size restriction
HttpResponseCache responseCache = new HttpResponseCache(
cacheDir,
10 * 1024 * 1024);
// Prepare OkHttp
httpClient = new OkHttpClient();
httpClient.setResponseCache(responseCache);
// Build Picasso with this custom Downloader
new Picasso.Builder(getContext())
.downloader(new OkHttpDownloader(httpClient))
.build();
我没有测试过这个,但是服务器也有可能返回一个HTTP标头,指示OkHttp永远不会缓存。为了测试,我建议:
setDebugging(true)
;从磁盘重新加载图像时,您应该看到黄色标记。答案 1 :(得分:0)
自:
How to implement my own disk cache with picasso library - Android?
//this code from https://developer.android.com/reference/android/net/http/HttpResponseCache.html
try {
File httpCacheDir = new File(context.getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
}catch (IOException e) {
Log.i(TAG, "HTTP response cache installation failed:" + e);
}
启用调试以检查缓存是否正常工作 。Picasso.with(的getContext())setDebugging(真);