I am using Picasso.
I want to cache images on disk and I am using okhttpdownloader for it.
Here is what I am trying to do I want Image to be cached on either external sd card or phone memory but don't want to cache images on heap memory due to memory constraints. So if image is loaded for first time it should be cached on disk and when next time request is made for same url it should look into disk first if it is cached or not.
I am not able to achieve this following is my code.
public class PicassoCache {
/**
* Static Picasso Instance
*/
private static Picasso picassoInstance = null;
private File cacheDir;
/**
* PicassoCache Constructor
*
* @param context application Context
*/
private PicassoCache (Context context) {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = context.getExternalCacheDir();
else
cacheDir = context.getCacheDir();
Downloader downloader = new OkHttpDownloader(cacheDir, Integer.MAX_VALUE);
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(downloader);
picassoInstance = builder.build();
}
/**
* Get Singleton Picasso Instance
*
* @param context application Context
* @return Picasso instance
*/
public static Picasso getPicassoInstance (Context context) {
if (picassoInstance == null) {
new PicassoCache(context);
return picassoInstance;
}
return picassoInstance;
}
}
and inside Adapter I have following code
PicassoCache.getPicassoInstance(context)
.load(item.imageUrl)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(holder.itemImage);
Try removing
.networkPolicy(NetworkPolicy.NO_CACHE)
and adding NO_STORE to memoryPolicy
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)