1

I am using Picasso in my android application for image caching. But whenever i open app the images starts loading from the server. But i want to use cached images from disk and simultaneously it should check for new image on server as well. Same like whatsapp do.

If i am using Network Policy to Offline it doesn't load the new image from server it just using the offline images.

url = Static.url+"users/image?id="+id;
    profileImg = R.drawable.user;
    Picasso.with(activity)
            .load(url)
            .placeholder(profileImg)
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() {
                    Picasso.with(activity)
                            .load(url)
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .placeholder(profileImg)
                            .error(profileImg)
                            .into(imageView);
                }
                @Override
                public void onError() {
                    // Try again online if cache failed
                }
            });

The above code is working but image loading is still not very much quick. The image should be immediately loaded from cache and also it should check online at the same moment for new image.

Please guide.

Suresh
  • 11
  • 2

5 Answers5

1

You can use this workaround:

Picasso.with(context)
                    .load(Uri.parse("https://yoururl.com"))
                    .into(holder.storyBigThumb, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {
                            // Try again online if cache failed
                            Picasso.with(context)
                                    .load(Uri.parse("https://yoururl.com"))
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .placeholder(R.drawable.user_placeholder)
                            .error(R.drawable.user_placeholder_error)
                                    .into(holder.storyBigThumb);
                        }
                    });

Picasso will try to get a new image from the server, if it fails, it will load it from the cache

  • The image should be loaded from cache first then should check online, but it is not loading from cache.. – Suresh Nov 19 '16 at 09:09
1

You can use okhttp for this purpose.

Add compile 'com.squareup.okhttp:okhttp:2.4.0' in the build.gradle file.

Make a class which extends the Application. eg :

public class YourApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Picasso.Builder builder = new Picasso.Builder(this);
        builder.downloader(new OkHttpDownloader(this, Integer.MAX_VALUE));
        Picasso built = builder.build();
        built.setIndicatorsEnabled(true);
        built.setLoggingEnabled(true);
        Picasso.setSingletonInstance(built);
    }
}

Now you can simply use the below code in the Activity :

Picasso.with(YourActivity.this).load(imageLink).networkPolicy(NetworkPolicy.OFFLINE).into(cover_image, new Callback() {
                    @Override
                    public void onSuccess() {                        }
                    @Override
                    public void onError() {
                        Picasso.with(YourActivity.this).load(imageLink).into(cover_image);
                    }
                });

And that all. I think the above code can be easily understood.

Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
1

First of all Picasso catches images automatically, and second if you want to do that load offline then try to load online thing, you need to use two calls for image load, first one using DiskPolicy to load from disk and second one using NetworkPolicy to force the call to load and catch from web.

M. Erfan Mowlaei
  • 1,376
  • 1
  • 14
  • 25
1

It's inefficient to do the first load if your device is offline. Just check the network connectivity before loading and then apply the right networkPolicy (offline or not).

RequestCreator requestCreator = Picasso.with(context).load(url);
if (!Utils.isNetworkAvailable(context)) {
    // offline mode: load from cache directly
    requestCreator.networkPolicy(NetworkPolicy.OFFLINE);
}
requestCreator.into(imageView);

isNetworkAvailable function:

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    // if no network is available networkInfo will be null
    // otherwise check if we are connected
    return networkInfo != null && networkInfo.isConnected();
}
glucas
  • 259
  • 2
  • 8
0

Picasso automatically caches the Images. https://futurestud.io/tutorials/picasso-influencing-image-caching

Vinay KG
  • 39
  • 1
  • 8