0

I'm using the following code to load an URL to an ImageView. It first tries to load from cache, and if it fails it tries to fetch it from internet:

Picasso.with(getActivity())
.load(imageUrl)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {
        //Try again online if cache failed
        Picasso.with(getActivity())
                .load(imageUrl)
                .error(R.drawable.error_image)
                .into(imageView, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                Log.v("Picasso","Could not fetch image");
            }
        });
    }
});

It works just fine, but the problem is that is really cumbersome to write all this code every time I have to load an image, compared to the standard:

Picasso.with(getContext())
 .load(imageUrl)
 .into(imageView);

Is there a way to encapsulate this behaviour ? Does Picasso provide any way to help it?

FlyingPumba
  • 1,015
  • 2
  • 15
  • 37
  • 4
    If you're always going to retry, why are you trying with `OFFLINE` in the first place? It'll use the cached version if available by default. – ianhanniballake Dec 14 '16 at 19:19
  • @ianhanniballake I didn't know that. I was using this answer http://stackoverflow.com/a/30686992/2271834 and so I assumed that cache usage was explicit – FlyingPumba Dec 14 '16 at 19:26

2 Answers2

1

try using this code this worked for me

this class uses a retry policy to download images

public class PicassoHelper {

private static final boolean isDebug = false;
private static final int MAX_RETRY_TIME = 10;         // Default is 3 in Picasso
private static final int MAX_DOWNLOADING_THREAD = 4;  // Recommand in Volley , it is 4
private static Picasso sPicasso;

public static Picasso Pwith(Context context) {
    // Mimicking Picasso's new OkHttpLoader(context), but with our custom OkHttpClient
    if (sPicasso == null) {
        OkHttpClient client = new OkHttpClient();
        client.setRetryOnConnectionFailure(true);
        // Create A Retry Policy
        client.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request request = chain.request();
                // try the request
                Response response = chain.proceed(request);
                int tryCount = 0;
                while (!response.isSuccessful() && tryCount < MAX_RETRY_TIME) {
                    Log.d("intercept :"," Request is not successful - " + tryCount);
                    tryCount++;
                    // retry the request
                    response = chain.proceed(request);
                }
                // otherwise just pass the original response on
                return response;
            }
        });
        sPicasso = new Picasso.Builder(context)
                .executor(Executors.newFixedThreadPool(MAX_DOWNLOADING_THREAD))
                .downloader(new OkHttpDownloader(client)).build();
        if(isDebug) {
            sPicasso.setIndicatorsEnabled(true);
            sPicasso.setLoggingEnabled(true);
        }
    }
    return sPicasso;
}

}


Now after creating the class its time to use it in out code

PicassoHelper.Pwith(application_context).load(image_url).into(imageView);

msmukesh4
  • 589
  • 4
  • 7
0

@ianhanniballake comment was right, and this SO answer is misleading. To enable caching, you only have to add to your Application:

Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this, Integer.MAX_VALUE));
Picasso.setSingletonInstance(builder.build());

And then Picasso/OkHttp takes care of looking in the cache before it tries to load an image from internet.

Community
  • 1
  • 1
FlyingPumba
  • 1,015
  • 2
  • 15
  • 37