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

你可能感兴趣的:(AsyncTask异步任务获取网络图片)