Search code examples
androidpicasso

Load image with Picasso when offline using SSL


I actually use Picasso in my project. The problem is : it seem there is not cache so the image are reloading all the time the url (https).

I would like to have the images in the disk cache. I want the cache of the picture when the image was already downloaded, not depending if the WIFI is off or not.

I have created a class (PicassoTrustAll.class) to get my images from a HTTPS url :

    public class PicassoTrustAll {

        private static Picasso mInstance = null;

        private PicassoTrustAll(Context context) {
            OkHttpClient client = new OkHttpClient();
            client.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s) throws java.security.cert.CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s) throws java.security.cert.CertificateException {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[] {};
                }
            } };
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                client.setSslSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }

            mInstance = new Picasso.Builder(context)
                    .downloader(new OkHttpDownloader(client))
                    .listener(new Picasso.Listener() {
                        @Override
                        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                            Log.e("PICASSO", String.valueOf(exception));
                        }
                    }).build();

        }

        public static Picasso getInstance(Context context) {
            if (mInstance == null) {
                new PicassoTrustAll(context);
            }
            return mInstance;
        }
}

And I use it like this :

PicassoTrustAll.getInstance(v.getContext())
                .load(image_url)
                .placeholder(R.drawable.progress_animation)
                .into(photo);

Solution

  • It's working using OKHTTP :

    Picasso.with(context).load("url")
                            .networkPolicy(NetworkPolicy.OFFLINE) 
                            .into(photo, new Callback() {
                                @Override 
                                public void onSuccess() { 
    
                                } 
    
                                @Override 
                                public void onError() {
                                    Picasso.with(context) 
                                            .load("url") 
                                    .placeholder(R.mipmap.ic_launcher)
                                    .error(R.drawable.user_error)
                                            .into(picture); 
                                } 
                            });