完美解决imageloader加载https图片

当我们使用开源框架UniversalImageLoader加载图片时,如果图片链接是https的话,就会显示不了图片,显示一片空白;

并且在android手机中的浏览器上访问该图片会提示网站安全证书已过期或不可信是否继续浏览的提示,如图:


解决方案一

设置请求支持https:
我们应该实现 ImageDownloader ,然后把它设置给ImageLoader的configuration
代码如下:

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;
        }
    }

然后在ImageLoaderConfiguration中设置config.imageDownloader(new AuthImageDownloader(getApplicationContext()))

ImageLoaderConfiguration config = new ImageLoaderConfiguration
			.Builder(getApplicationContext())
			.defaultDisplayImageOptions(defaultOptions)
            .threadPriority(Thread.NORM_PRIORITY - 2)
            .denyCacheImageMultipleSizesInMemory()
            .diskCacheFileNameGenerator(new Md5FileNameGenerator())
            .imageDownloader(new AuthImageDownloader(this))
            .tasksProcessingOrder(QueueProcessingType.LIFO).build();

这样就完美解决了imageloader可以加载https图片

解决方案二:
有人已经修改universalimageloader了,把这些配置弄好了,然后重新打包为universalimageloader,jar,解决图片路径中有https时Imageloader报出异常java.security.cert.CertPathValidatorException: 
Trust anchor for certification path not found.使https图片完美显示
我们只要使用这个jar包就可以了
下载地址: http://download.csdn.net/detail/wk843620202/9472469


你可能感兴趣的:(https,ImageLoader)