0

I am trying to cache picture using picasso after doing some home work I figured out picasso doesn't direct cache images. Hence using help from https://gist.github.com/fada21/10655652

This is sucessfully caching image to folders. I can see the files but they don't reload when the phone is offline.

I am loading the images this way:

PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • what do you mean by **reload when the phone is offline** – Derek Fung Aug 31 '15 at 15:17
  • I am what to load image from cache when the phone doesn't have internet. – TheDevMan Aug 31 '15 at 15:18
  • The fundamental: Picasso has memory cache itself, OKHttp has disk cache. What you want is something like disk cache, however OKHttp cache following http standard, it has expiry time and it would issue http request to check if the cache is still valid. If you own the website, you should config it properly around caching of http. If not, you have to implement your own caching mechanism. – Derek Fung Aug 31 '15 at 15:25
  • possible duplicate of [Load images from disk cache with Picasso if offline](http://stackoverflow.com/questions/23391523/load-images-from-disk-cache-with-picasso-if-offline) – njzk2 Aug 31 '15 at 15:31

1 Answers1

0

I fixed the loading from cache issue like this:

I used https://gist.github.com/fada21/10655652

And then did the following:

added a variable to check if internet is exists or not...

if (isnetworkavailable.equalsIgnoreCase(mContext.getResources().getString(R.string.yesnetwork)))
{ 
 PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
}
else
{
 PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).networkPolicy(NetworkPolicy.OFFLINE).into(holder.image);
}

This works fine if I close the app with system.exit or force close.

Not sure if this is right way but I am getting the results. Things are working smoothly. Hope this is useful for somebody!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144