内容如题,在glide加载某些https链接时,会报ssl exception错误,原因是他们的链接是处于不信任类型(不安全),所以会报错,导致图片加载失败。
环境:
AndroidStudio fox
win10
glide4.0+
解决方法如下:
(一)引入相关依赖
"glide" : "com.github.bumptech.glide:glide:4.9.0",
"glide-compiler" : "com.github.bumptech.glide:compiler:4.9.0",
"glide-okhttp3-integration" : "com.github.bumptech.glide:okhttp3-integration:4.9.0",
api dp["glide"]
api dp["glide-okhttp3-integration"]
kapt dp["glide-compiler"]
build.gradle还需要申明kotlin相关依赖:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
(二)自定义AppGlideModule
这里通过继承AppGlideModule,重写其中方法,设置所有请求均是"信任",允许访问,从而解决标题出现的问题。
直接上代码:
GlideCache.java
/**
* 若要使用自定义module,外部必须使用glideapp进行调用
*/
@GlideModule
public class GlideCache extends AppGlideModule {
private String appRootPath = null;
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//手机app路径--目前该路径是不用权限即可保存的路径
appRootPath = context.getFilesDir().getPath();
Log.d("glide", "glide 图片缓存路径:" + appRootPath);
// 100 MB
int diskCacheSizeBytes = 1024 * 1024 * 100;
builder.setDiskCache(new DiskLruCacheFactory(appRootPath + "/GlideDisk", diskCacheSizeBytes));
}
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
OkHttpClient client = GlideUnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.north.light.libloadpic">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application>
<meta-data
android:name="com.north.light.libloadpic.glide.GlideCache"
android:value="AppGlideModule" />
</application>
</manifest>
GlideUnsafeOkHttpClient.java
public class GlideUnsafeOkHttpClient {
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对象,直接使用glideapp对象,示例如下:
至此,完成。
注意点:
(1)AppGlideModule继承,重写,申明
(2)加载图片时,加载图片对象的调用(GlideApp)
that's all---------------------------------------------------------------------