Glide库依赖:

implementation 'com.github.bumptech.glide:glide:4.0.0'

一些基本的使用方法和老版本差别不大,这里主要记录一些花了一定时间才解决的进阶功能。

1.https资源加载失败,报错信息:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
很明显是ssl证书验证失败,解决方法就是设置https证书或者不进行验证,由于没有证书所以就不进行验证了,方法是使用okhttpClient来设置证书,也可以设置超时时间。如下:
1.首先新建一个类,需要继承AppGlideModule,并重写registerComponents方法:

@GlideModule
public class MyAppGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if(activityManager != null){
            builder.setDefaultRequestOptions(ImageUtil.getOption());
        }
    }


    @Override
    public void registerComponents(Context context, Glide glide , Registry registry) {
    //设置请求方式为okhttp 并设置okhttpClient的证书及超时时间
        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(UnsafeOkHttpClient.getUnsafeOkHttpClient());
        registry.replace(GlideUrl.class, InputStream.class,factory);
    }
 //自定义工具类修改OkHttpClient证书和超时时间
  static 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);
            builder.writeTimeout(20,TimeUnit.SECONDS);

            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 禁止解析Manifest文件
     * 主要针对V3升级到v4的用户,可以提升初始化速度,避免一些潜在错误
     * @return
     */
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}

其中OkHttpUrlLoader这个类需要增加一个依赖,否则找不到:

  implementation("com.github.bumptech.glide:okhttp3-integration:4.0.0" ) {
        exclude group: "com.android.support"
    }

注意点:Glide 4.0+ 使用AppGlideModule 替代原来的GlideModule,并且使用@GlideModule注解动态注册,取代老版本在manifest中注册的方式。但是有的人可能会遇到AppGlideModule 设置内容 未生效的问题,而且也使用了@GlideModule的注解,而且注解不报错,我觉得这个算是一个坑了,原因是需要增加一个依赖这个注解才能生效:

annotationProcessor  'com.github.bumptech.glide:compiler:4.0.0'
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐