当前位置: 首页 > news >正文

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"><Buttonandroid:id="@+id/btn_show"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="30dp"android:text="图片显示"></Button><ImageViewandroid: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() {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic 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() {@Overridepublic 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;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.t_picture);//设置上下文mContext=WebPictureActivity.this;//获取ImageViewimageView = findViewById(R.id.iv1);//获取按钮button = findViewById(R.id.btn_show);//设置按钮点击响应button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//创建进程Th
http://www.lryc.cn/news/295272.html

相关文章:

  • 跟着GPT学设计模式之原型模式
  • 博客|基于Springboot的个人博客系统设计与实现(源码+数据库+文档)
  • 【gcc】webrtc发送侧计算 丢包率
  • elementui上传文件不允许重名
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Video媒体组件
  • Linux操作系统运维-Docker的基础知识梳理总结
  • PMP考试成绩如何查询?
  • 【Scala】 2. 函数
  • 14.0 Zookeeper环球锁实现原理
  • 课时16:本地变量_普通变量
  • 阿里云服务器centos_7_9_x64位,3台,搭建k8s集群
  • 代码随想录第二十八天
  • 【python】绘制爱心图案
  • 在 Elastic Agent 中为 Logstash 输出配置 SSL/TLS
  • Vue中对虚拟DOM的理解
  • golang通用后台管理项目——Go+Vue通用后台管理项目实战
  • 推动海外云手机发展的几个因素
  • python coding with ChatGPT 打卡第17天| 二叉树:找树左下角的值、路径总和
  • 2020年通信工程师初级 综合能力 真题
  • 12.0 Zookeeper 数据同步流程
  • 作业2.6
  • Qt应用软件【协议篇】TCP示例
  • C# CAD交互界面-自定义面板集(四)
  • 物流自动化移动机器人|HEGERLS三维智能四向穿梭车助力优化企业供应链
  • EasyExcel下载带下拉框和批注模板
  • C语言之字符逆序(牛客网)
  • RAPTOR:树组织检索的递归抽象处理
  • 图论:合适的环
  • 【数据分享】1929-2023年全球站点的逐月平均降水量(Shp\Excel\免费获取)
  • React+Antd实现省、市区级联下拉多选组件(支持只选省不选市)