Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide V4 load https images

I know this link, and tried but this is for Glide V3 solution, I need to load https://myimage/image/xxx.png but glide throw exception

FileNotFoundException(No content provider)** and **SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found

I am using 4.5.0 Glide version, I have spend a lot of time but can't find any solution, any help will be appreciated.

like image 898
Mohit Suthar Avatar asked Mar 29 '18 13:03

Mohit Suthar


2 Answers

I am struggling near about 1 day but find solution, if you are using ssl certified link like https then you need to add two more dependency of glide so you have to add this

implementation 'com.github.bumptech.glide:annotations:4.5.0'
kapt 'com.github.bumptech.glide:compiler:4.5.0'

I am using kotlin thats why i added kapt you can use annotationProcessor

After that you need to create GlideModule in project, so here is the code for Glide V4

 @GlideModule
public class MyGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        super.applyOptions(context, builder);
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        OkHttpClient okHttpClient= UnsafeOkHttpClient.getUnsafeOkHttpClient();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
    }
}

here we are bypass the ssl so here is the UnsafeOkHttpClient class

    public class UnsafeOkHttpClient {


    public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

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

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 }

after that you need two more class OkHttpUrlLoader and OkHttpStreamFetcher

you can copy past from Glide Link

After that final step which i am dont know thats why it will take one day so you need to build project and Glide will generate GlideApp class, so you need to use that GlideApp class to show image HTTPS like this:

GlideApp.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView)

I think this is very help full for others which are suffering this typeof issues. Keep Code :)

like image 141
Mohit Suthar Avatar answered Oct 01 '22 18:10

Mohit Suthar


Structure of the answer of Mohit works fine, however right now you can take all the required classes here So to include them just add

implementation "com.github.bumptech.glide:okhttp3-integration:$glideV"
like image 23
Nazacheres Avatar answered Oct 01 '22 16:10

Nazacheres