项目中使用到glide加载网络https图片。这里把遇到的问题记录一下。
1. 在加载图片的时候一般会设置占位图片 ,我们会加一个placeholder (loadingImage)如下:
Glide.with(mContext).load(path).placeholder(loadingImage).error(errorImageView).into(mImageView);
但是会发现一直显示占位符的图片,不显示path 的路径, 这个原因应该是缓存问题。只要加上dontAnimate() 或者把placeholder (loadingImage)的loadingimage 换成mImageview.getDrawable如下:
Glide.with(mContext).load(path).placeholder(loadingImage).dontAnimate().error(errorImageView).into(mImageView);
2. 加载https图片报错:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
这个报错信息是在监听中得到的:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public void glideLoad(final Activity activity, String url, final ImageView imageView, int default_image) {
if (!activity.isDestroyed()) {
Glide.with(activity).load(url).placeholder(default_image).dontAnimate().listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
}).into(imageView);
}
}
虽然我们在网络请求中已经添加了忽略证书来访问https请求的逻辑,但是glide却加载不出来网络图片,这是因为glide使用的是自身的请求框架,所以需要针对glide进行处理。
处理方法:
在代码中添加以上三个类, 然后在Application中初始化。
//让Glide能用HTTPS
Glide.get(this).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(NetUtils.genericIgnoreClient(this)));
然后就可以正常加载https的图片了。