AsyncTask异步任务获取网络图片

AndroidManifest.xml权限:

 <uses-permission android:name="android.permission.INTERNET" />

布局activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp" />

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private Button button;
    private ImageView imageView;
    private ProgressDialog dialog;
    private String imagePath = "https://img.tukuppt.com//png_preview/00/03/60/oHACmBtR5V.jpg!/fw/780";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        imageView = findViewById(R.id.imageView);
        dialog = new ProgressDialog(this);
        dialog.setTitle("提醒");
        dialog.setMessage("下载中,请稍后");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               new MyTask().execute(imagePath);
            }
        });
    }
    /***
     * 第一个参数:表示要执行的通常是网络路径
     * 第二个参数:表示进度的刻度
     * 第三个参数:表示执行任务的返回值
     * **/
    class MyTask extends AsyncTask<String,Void,Bitmap>{
        //表示任务执行前的操作
        @Override
        protected void onPreExecute(){
           super.onPreExecute();
           dialog.show();
        }
        //主要完成耗时操作
        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap bitmap = null;
            try {
                URL url = new URL(strings[0]);
                //使用网络连接类HttpURLConnection完成对网络数据的提取
                //得到connection对象。
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                int length = connection.getContentLength();
                //设置请求方式
                connection.setRequestMethod("GET");
                //连接
                connection.connect();
                int responseCode = connection.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK){
                    InputStream is = connection.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, length);
                    bitmap = BitmapFactory.decodeStream(bis);
                    bis.close();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap result){

            super.onPostExecute(result);
            imageView.setImageBitmap(result);
            dialog.dismiss();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值