0313-使用HttpURLConnection加载网络图片

这篇博客介绍了如何在Android中使用HttpURLConnection异步加载网络图片,通过新建ImgLoadTask类提高加载速度和代码复用性。步骤包括创建Activity布局,设置按钮点击事件执行加载任务,以及实现LoadImgTask类来处理网络请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加载网络图片,需要用到Bitmap对象。
加载网络图片有时也需耗时较长时间,所以也应该异步加载。
新建一个外部的ImgLoadTask类(可以更快速加载、提高代码复用性)

1.首先,新建一个Activity,布置其xml代码:

一个按钮Button
一个ImageView加载图片的地方

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.test.project.netconnection.NetPicActivity3">

    <Button
        android:id="@+id/showBtn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="20sp"
        android:text="加载网络图片"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/image"
        />
</LinearLayout>

2.布置java代码:

在按钮点击事件里面加载LoadImgTAsk
用execute方法传入网络图片地址

    private Button button;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_pic3);
        bindId();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                LoadImgTAsk loadImgTAsk=new LoadImgTAsk(imageView);
                loadImgTAsk.execute("http://n1.itc.cn/img8/wb/recom/2016/08/17/147140092923156017.JPEG");
            }
        });
    }


    private void bindId() {

        button=findViewById(R.id.showBtn);
        imageView=findViewById(R.id.imageView);

    }

3.异步任务类LoadImgTAsk 代码:

public class LoadImgTAsk  extends AsyncTask<String,Integer,Bitmap>{

    private ImageView imageView;
    public LoadImgTAsk(ImageView imgview){
        this.imageView=imgview;

    }

    @Override
    //图片需要返回Bitmap形式
    protected Bitmap doInBackground(String... strings) {
        Bitmap bt=null;
        try {
             //创建URL对象
            URL url=new URL(strings[0]);
            //用URL对象获取openConnection方法
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            //获取输入流
            InputStream inputStream=httpURLConnection.getInputStream();
            //Bitmap对象获取图片输入流
            bt= BitmapFactory.decodeStream(inputStream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bt;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        imageView.setImageBitmap(bitmap);
    }
}

效果图

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值