Glide V4加载https图像 [英] Glide V4 load https images

查看:130
本文介绍了Glide V4加载https图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个链接,并尝试过,但这是针对Glide V3解决方案的,我需要加载 https://myimage/image/xxx.png 但滑行抛出异常

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

我正在使用4.5.0 Glide版本, 我花了很多时间,但是找不到任何解决方案,我们将不胜感激.

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.

推荐答案

我在大约1天的时间里苦苦挣扎,但是找到了解决方案,如果您使用的是https之类的ssl认证链接,则需要添加两个以上的glide依赖项,添加

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'

我正在使用kotlin,这就是为什么我添加 kapt 可以使用 annotationProcessor

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

之后,您需要在项目中创建GlideModule,所以这是 Glide V4

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));
    }
}

这里我们绕过了ssl,所以这里是 UnsafeOkHttpClient

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);
    }
}

}

之后,您需要再添加两个类 OkHttpUrlLoader和OkHttpStreamFetcher

after that you need two more class OkHttpUrlLoader and OkHttpStreamFetcher

您可以从滑动链接

在最后一步之后,我不知道为什么要花一天的时间,因此您需要构建项目,而Glide会生成 GlideApp 类,因此您需要使用该 GlideApp类显示图片HTTPS 像这样:

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 :)

这篇关于Glide V4加载https图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆