Android glide在Android 4.X中无法加载图片。

huangapple go评论110阅读模式
英文:

Android glide not loading pictures in Android 4.X

问题

以下是您要翻译的内容:

我正在使用Glide v4.11从网络加载图片,在Android 5.0或更高版本中一切正常,但在Android 4.X中图片不加载。

以下是我的代码:

RequestOptions options = new RequestOptions()
        .placeholder(android.R.drawable.progress_horizontal)
        .error(android.R.drawable.presence_offline)
        .diskCacheStrategy(DiskCacheStrategy.ALL);

Glide.with(context)
        .load(data.getImage_url())
        .apply(options)
        .into(holder.thumbnail);

以下是Android 4.4上的LogCat:

W/Glide: 加载 https://www.gradientapi.xyz/generate/w/1000/h/500 失败,尺寸为 [188x263]
    class com.bumptech.glide.load.engine.GlideException: 无法加载资源
    原因之一:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL 握手中止:ssl=0xb85422a0:SSL 库中的故障,通常是协议错误
    错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议 (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     有关更多细节,请调用 GlideException#logRootCauses(String)
      原因之一(共 1 个):class com.bumptech.glide.load.engine.GlideException: 获取数据失败,类为 java.io.InputStream,远程
    原因之一:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL 握手中止:ssl=0xb85422a0:SSL 库中的故障,通常是协议错误
    错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议 (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     有关更多细节,请调用 GlideException#logRootCauses(String)
        原因之一(共 1 个):class com.bumptech.glide.load.engine.GlideException: 获取失败
    原因之一:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL 握手中止:ssl=0xb85422a0:SSL 库中的故障,通常是协议错误
    错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议 (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     有关更多细节,请调用 GlideException#logRootCauses(String)
          原因之一(共 1 个):class javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL 握手中止:ssl=0xb85422a0:SSL 库中的故障,通常是协议错误
    错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议 (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000)

我在网上搜索了一下,发现 Android 4.4 仅支持 TLS 1.0。

这可能是问题吗?如果是,是否有任何解决方案?
英文:

I am using Glide v4.11 to load pictures from the web , everything work fine in android 5.0 or higher but in android 4.X pictures do not load.

Here is my code :

 RequestOptions options = new RequestOptions()
                    .placeholder(android.R.drawable.progress_horizontal)
                    .error(android.R.drawable.presence_offline)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);

   Glide.with(context)
                    .load(data.getImage_url())
                    .apply(options)
                    .into(holder.thumbnail);

and here is the LogCat on Android 4.4:

W/Glide: Load failed for https://www.gradientapi.xyz/generate/w/1000/h/500 with size [188x263]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
    There was 1 cause:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85422a0: Failure in SSL library, usually a protocol error
    error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     call GlideException#logRootCauses(String) for more detail
      Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
    There was 1 cause:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85422a0: Failure in SSL library, usually a protocol error
    error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
    There was 1 cause:
    javax.net.ssl.SSLHandshakeException(javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85422a0: Failure in SSL library, usually a protocol error
    error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000))
     call GlideException#logRootCauses(String) for more detail
          Cause (1 of 1): class javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85422a0: Failure in SSL library, usually a protocol error
    error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0xa744e990:0x00000000)

I searched over web and i found that Android 4.4 only support TLS 1.0.

It may be the issue ? and if yes is there any solutions ?

答案1

得分: 1

这是一个握手异常。在Android 5.0之前的设备上,默认情况下不启用TLSv1.1TLSv1.2协议,因此会出现这个异常。

你可以尝试在你的应用程序类的onCreate方法内添加以下代码:

if (Build.VERSION.SDK_INT == 19) {
    try {
        ProviderInstaller.installIfNeeded(this);
    } catch (Exception ignored) {
    }
}

另外,你可以查看这个GitHub问题以获取更好的解决方案。

英文:

It's a handshake exception. On devices older than Android 5.0 TLSv1.1 and TLSv1.2 protocols are not enabled by default and that's why this exception occurs.

You can try this inside your Application's class onCreate method:

if (Build.VERSION.SDK_INT == 19) {
        try {
            ProviderInstaller.installIfNeeded(this);
        } catch (Exception ignored) {
        }
    }

Also, You can check this github issue for better solutions.

答案2

得分: 0

我在API 30上遇到了这个问题。很奇怪,我一直在使用Retrofit和okhttp来访问同一台服务器上的其他内容,但当我尝试使用Glide加载图像时,我遇到了可怕的错误:

SSLProtocolException:读取错误:ssl=0xe78f6a18:SSL库中的失败,通常是协议错误

我看到有关早期Android版本在TLS1.2上存在问题的各种帖子,但这并不是问题的原因。似乎Glide依赖于较旧版本的OKHttp,这就是问题所在。或者类似于这样的问题,不太确定,但我确实在我的gradle文件中通过以下方式修复了它:

implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'

似乎 okhttp3-integration 是解决此问题的关键。我在这里发布这个帖子,供其他遇到这个问题并进行搜索的人参考。

英文:

I ran into this problem on API 30. It was weird, I was using Retrofit with okhttp just fine to access other things on the same server, but when I tried to use Glide to load an image, I got the dreaded:

SSLProtocolException: Read error: ssl=0xe78f6a18: Failure in SSL library, usually a protocol error

I saw various posts about much older Android versions having problems with TLS1.2 but that wasn't the issue. It seems like Glide has a dependency on an older version of OKHttp and that was the problem. Or something like that, not exactly sure, but I did fix it with this in my gradle file:

implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'

It seems like the okhttp3-integration is what fixes it. I'm posting this here for anyone else who gets stumped by this and does some searches.

huangapple
  • 本文由 发表于 2020年4月4日 07:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定