相信大家在[吉林快_三源码下载](http://haozbbs.com)开发app过程中,肯定会接触到第三方加载图片工具(比如Glide,ImageLoader,vollery,xutils等等)
Glide这里不做讲解,自己也没怎么用过,详情可以了解郭霖的博客 https://blog.csdn.net/guolin_blog/。
vollery框架,博主也没使用过,xutils光听说过,也没用过。
现在只能讲解Imageloader的加载呢。
ImageLoader简介
ImageLoader是一个加载图片的开源框架,其基本功能为加载本地和网络图片。
github地址:https://github.com/nostra13/Android-Universal-Image-Loader
详细了解可以从此链接进入:https://blog.csdn.net/wl1769127285/article/details/52786717
ImageLoader 使用
在后来的版本中省去了初始化的过程,可以直接使用
下边介绍其主流的几个方法:
displayImage
/**
* Adds display image task to execution pool. Image will be set to ImageView when it's turn.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param imageView {@link ImageView} which should display image
* @param options {@linkplain DisplayImageOptions Display image options} for image displaying. If <b>null</b> -
* default display image options
* {@linkplain ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
* configuration} will be used.
* @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called before
* @throws IllegalArgumentException if passed <b>imageView</b> is null
*/
public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) {
displayImage(uri, imageView, options, null);
}
loadImage
/**
* Adds load image task to execution pool. Image will be returned with
* {@link ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} callback}.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param minImageSize Minimal size for {@link Bitmap} which will be returned in
* {@linkplain ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} callback}. Downloaded image will be decoded
* and scaled to {@link Bitmap} of the size which is <b>equal or larger</b> (usually a bit larger) than
* incoming minImageSize .
* @param listener {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
* @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called before
*/
public void loadImage(String uri, ImageSize minImageSize, ImageLoadingListener listener) {
loadImage(uri, minImageSize, null, listener);
}
上边ImageLoader的方法简单介绍了一下,回归正题:imageload加载https图片
如上图所示:初始化的时候,只需要引入一个加载项 .imageDownloader()
加载项里边的实例如下:
package com.nuctech.handheldlock.https;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import android.content.Context;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
public class AuthImageDownloader extends BaseImageDownloader {
private SSLSocketFactory mSSLSocketFactory;
public AuthImageDownloader(Context context) {
super(context);
SSLContext sslContext = sslContextForTrustedCertificates();
mSSLSocketFactory = sslContext.getSocketFactory();
}
public AuthImageDownloader(Context context, int connectTimeout,
int readTimeout) {
super(context, connectTimeout, readTimeout);
SSLContext sslContext = sslContextForTrustedCertificates();
mSSLSocketFactory = sslContext.getSocketFactory();
}
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra)
throws IOException {
URL url = null;
try {
url = new URL(imageUri);
} catch (MalformedURLException e) {
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(mSSLSocketFactory);
((HttpsURLConnection) conn).setHostnameVerifier((DO_NOT_VERIFY));
}
return new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE);
}
// always verify the host - dont check for certificate
final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public SSLContext sslContextForTrustedCertificates() {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
// javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} finally {
return sc;
}
}
class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
}
这样初始化之后就可以直接调用了