Android:多线程下载网络图片
3.12 网络图片操作
1、通过URL请求获取网络图片
示例:
创建t_picture.xml,页面layout布局文件,一个Button按钮和一个ImageView容器显示图片。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="图片显示">
</Button>
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
创建WebPictureActivity继承Activity,页面对应的Activity文件。loadWebPicture:加载网络图片,注意需要在新的Thread调用网络请求。
创建URL类对象;
调用URL的openConnection方法获取连接HttpURLConnection类对象connection;
调用connection对象的getInputStream获取输入流;
调用类BitmapFactory的decodeStream方法,通过输入流创建位图。
注意:
1、进行Https请求时,报错:javax.net.ssl.SSLHandshakeException,创建handleSSLHandshake方法,在onCreate方法中调用。
//报错:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
//Https请求的验证证书不支持,所有证书验证通过
public static void handleSSLHandshake(){
TrustManager[] trustManagers=new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
try {
SSLContext sslContext=SSLContext.getInstance("TLS");
//信任所有证书
sslContext.init(null,trustManagers,new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
//任何hostname都验证通过
return true;
}
});
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
2、进行Http请求时,报错:Cleartext HTTP traffic to img.pconline.com.cn not permitted,可以配置运行Http请求。
在res中创建xml文件夹,创建network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--允许进行http网络请求
在mainfests中配置application属性值
android:networkSecurityConfig="@xml/network_security_config"
-->
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在AndroidManifest.xml配置application属性networkSecurityConfig
android:networkSecurityConfig="@xml/network_security_config"
3、网络请求需要在AndroidManifest.xml配置permission。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
完整代码:
public class WebPictureActivity extends Activity {
private Context mContext;
private ImageView imageView;
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t_picture);
//设置上下文
mContext=WebPictureActivity.this;
//获取ImageView
imageView = findViewById(R.id.iv1);
//获取按钮
button = findViewById(R.id.btn_show);
//设置按钮点击响应
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建进程
Th