I'm using picasso to load images for my app. None are particularly large, but its only ever caching to memory so I'm running into out of memory errors on image heavy pages. Is there something I need to set manually in either picasso or the emulator to enable disk caching?
Asked
Active
Viewed 2,944 times
1
-
While the description mentions disk caching, I see nothing in the JavaDocs referencing it. – CommonsWare Sep 03 '13 at 23:58
-
How do you know its only caching in memory only? Did you try airplane mode on after loading a few images? (restart the process). Also call debugging(true) in your instance. Also, what Downloader is it using? OkHttpDownloader or UrlConnectionDownloader? It could be that the disk cache could not be installed. – dnkoutso Sep 04 '13 at 04:08
-
I assumed it was caching in memory only because I had set debugging(true) and was only seeing red and green tags. However I tried setting the emulator to airplane mode and turning off wifi on my computer after loading some images as you suggested, and when they reloaded after being off screen they would reappear with red tags. Perhaps it is caching to memory and just not displaying the correct debug indicators? Not sure what downloader, I'm targeting sdk 14+ so I assume OkHttpDownloader? – Igd Sep 04 '13 at 21:31
-
This might be a problem with emulator or headers are missing for caching. Did you attempt other urls other than your own? Perhaps the ones that ship with Picasso sample. – dnkoutso Jan 22 '14 at 15:45
2 Answers
2
Have you provided a custom Downloader into Picasso? There are a few things you should make sure of:
- Do you have permission to write to the specified cache folder?
- Is your cache size restriction large enough for the images Picasso is downloading?
Here is an example implementation for writing the images to the cache directory on the SD card:
// 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();
I haven't tested this, but there also exists the possibility that the server is returning an HTTP header instructing OkHttp to never cache. For testing, I'd suggest:
- Enable Picasso's
setDebugging(true)
; you should see a yellow marker when the image is reloaded from disk. - Kill your application when testing the cache; a green marker indicates it's coming from the memory cache.
- Download an image from a static location where you're certain the server isn't sending cache-expiry/no-pragma headers.

Paul Lammertsma
- 37,593
- 16
- 136
- 187
-
I have this code and the pics are in the Picasso cache dir but the log says: Sending progress READING_FROM_CACHE Cache content not available or expired or disabled Any idea? Thanks in advance. – wendigo May 12 '14 at 15:58
0
From:
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);
}
And enable debugging to check the cache is working Picasso.with(getContext()).setDebugging(true);

Community
- 1
- 1

jeremyvillalobos
- 1,795
- 2
- 19
- 39