0

Sorry for my english. I need load image from url, but url use HTTPS protocol. I try load image through android libruary ImageLoader and i have error:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
    javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

How i can load image in protocol HTTPS?

My example:

in main activity:

DisplayImageOptions mDisplayImageOptions = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.abc_ab_share_pack_mtrl_alpha)
                /*.showImageOnLoading(R.drawable.loading_bg)
                .showImageOnLoading(R.drawable.loading_bg)*/
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();

        ImageLoaderConfiguration conf = new ImageLoaderConfiguration.Builder(context)
                .defaultDisplayImageOptions(mDisplayImageOptions)
                .memoryCacheSize(50 * 1024 * 1024)
                .discCacheSize(50 * 1024 * 1024)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheExtraOptions(250, 250, null)
                .threadPoolSize(5)
                .writeDebugLogs()
                .build();


        mImageLoader = ImageLoader.getInstance();
        mImageLoader.init(conf);

And in adapter

imageLoader.displayImage(image.get(position).getLinkImge(),holder.image);
g8214435
  • 737
  • 2
  • 6
  • 19
  • 1
    **The server's SSL certificate is not signed by a trusted authority**. You either need a *valid* certificate for your server or implement a custom SSLSocketFactory for a custom image downloader class that will bypass certificate check if certain conditions are met. – Gergely Kőrössy Sep 04 '15 at 09:06
  • 1
    are you using a self signed certificate ?? – Panther Sep 04 '15 at 09:09
  • 1
    Check this out if it can help your issue http://stackoverflow.com/questions/21183043/sslhandshakeexception-trust-anchor-for-certification-path-not-found-android-http – Panther Sep 04 '15 at 09:10
  • @Panther i dont know, maby yes. I see example and add its to my code. I update my qestion – g8214435 Sep 04 '15 at 09:12
  • 1
    if u dont mind can you share the image url ? – Panther Sep 04 '15 at 09:16
  • @Panther thanks, I solved the problem – g8214435 Sep 04 '15 at 09:35

1 Answers1

7

Its work, need create class

public class SSLCertificateHandler {

    protected static final String TAG = "NukeSSLCerts";

    /**
     * Enables https connections
     */
    public static void nuke() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                    return myTrustedAnchors;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
        } catch (Exception e) {
        }
    }

}

and use like this:

SSLCertificateHandler.nuke();

and its work =)

g8214435
  • 737
  • 2
  • 6
  • 19