Glide 4.5加载https图片

作者: flywfk | 来源:发表于2019-02-18 18:26 被阅读5次
    一、引入库文件
    implementation 'com.github.bumptech.glide:glide:4.5.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
    //https图片处理
    implementation "com.github.bumptech.glide:okhttp3-integration:4.5.0"
    
    二、重写AppGlideModule
    /**
     * Created by wangfengkai on 2019/2/18.
     */
    @GlideModule
    public class OkHttpAppGlideModule  extends AppGlideModule {
    
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
    }
    }
    

    必须加上 @GlideModule注解,其中UnsafeOkHttpClient类的代码如下:

    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;
                }
            });
    
            builder.connectTimeout(20, TimeUnit.SECONDS);
            builder.readTimeout(20,TimeUnit.SECONDS);
    
            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    }
    
    三、配置glide加载https所需要的GlideModule
        <meta-data
            android:name="xxx.xxx.xxx.OkHttpAppGlideModule"
            android:value="AppGlideModule"/>
    
    四、混淆
    -keep public class * implements com.bumptech.glide.module.GlideModule
    -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
      **[] $VALUES;
      public *;
    }
    -keep public class * extends com.bumptech.glide.module.AppGlideModule
    -keep class com.bumptech.glide.GeneratedAppGlideModuleImpl { *; }

    相关文章

      网友评论

        本文标题:Glide 4.5加载https图片

        本文链接:https://www.haomeiwen.com/subject/krdzeqtx.html