Glide V4 加载 https 图片
Glide V4 load https images
我知道这个, and tried but this is for Glide V3 solution, I need to load https://myimage/image/xxx.png但是滑行抛出异常
FileNotFoundException(No content provider)** and **SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
我使用的是4.5.0 Glide版本,
我花了很多时间但找不到任何解决方案,我们将不胜感激。
我苦苦挣扎了将近 1 天,但找到了解决方案,如果您使用的是 ssl 认证 link,例如 https,那么您需要再添加两个 glide 依赖项,因此您必须添加这个
implementation 'com.github.bumptech.glide:annotations:4.5.0'
kapt 'com.github.bumptech.glide:compiler:4.5.0'
我正在使用 kotlin,这就是为什么我添加了 kapt 你可以使用 annotationProcessor
之后你需要在项目中创建GlideModule,所以这里是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 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);
}
}
}
之后你还需要两个 class OkHttpUrlLoader 和 OkHttpStreamFetcher
你可以从Glide Link
复制过去
在我不知道的最后一步之后,这就是为什么需要一天的时间,所以您需要 构建项目 并且 Glide 将生成 GlideApp class,所以你需要使用 GlideApp class 来显示图像 HTTPS
像这样:
GlideApp.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView)
我认为这对其他遭受此类问题的人来说非常有帮助。
保留代码:)
Mohit 的答案结构很好,但是现在您可以采取所有必需的 类 here
所以要包括它们只需添加
implementation "com.github.bumptech.glide:okhttp3-integration:$glideV"
这个解决方案对我有用
实施 'com.github.bumptech.glide:glide:4.8.0'
添加扩展应用程序
SSLContext mySSLContext = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
if (arg0.equalsIgnoreCase("YOUR_DOMAIN OR YOUR IP"))
return true;
else
return false;
}
});
我知道这个
FileNotFoundException(No content provider)** and **SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
我使用的是4.5.0 Glide版本, 我花了很多时间但找不到任何解决方案,我们将不胜感激。
我苦苦挣扎了将近 1 天,但找到了解决方案,如果您使用的是 ssl 认证 link,例如 https,那么您需要再添加两个 glide 依赖项,因此您必须添加这个
implementation 'com.github.bumptech.glide:annotations:4.5.0'
kapt 'com.github.bumptech.glide:compiler:4.5.0'
我正在使用 kotlin,这就是为什么我添加了 kapt 你可以使用 annotationProcessor
之后你需要在项目中创建GlideModule,所以这里是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 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);
}
}
}
之后你还需要两个 class OkHttpUrlLoader 和 OkHttpStreamFetcher
你可以从Glide Link
复制过去在我不知道的最后一步之后,这就是为什么需要一天的时间,所以您需要 构建项目 并且 Glide 将生成 GlideApp class,所以你需要使用 GlideApp class 来显示图像 HTTPS 像这样:
GlideApp.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView)
我认为这对其他遭受此类问题的人来说非常有帮助。 保留代码:)
Mohit 的答案结构很好,但是现在您可以采取所有必需的 类 here 所以要包括它们只需添加
implementation "com.github.bumptech.glide:okhttp3-integration:$glideV"
这个解决方案对我有用 实施 'com.github.bumptech.glide:glide:4.8.0'
添加扩展应用程序
SSLContext mySSLContext = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
if (arg0.equalsIgnoreCase("YOUR_DOMAIN OR YOUR IP"))
return true;
else
return false;
}
});