老羅Android(19)AsyncTask下載圖片例子

佈局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/download_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下載網絡圖片" />

</LinearLayout>
public class MainActivity extends Activity {

    Button mButton;
    ImageView mImageView;
    String path = "http://f.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=c8306d71f5deb48fef64a98c9176514c/0b55b319ebc4b74576634006c9fc1e178a82152e.jpg";
    ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView)this.findViewById(R.id.imageView1);
        mButton = (Button)this.findViewById(R.id.download_image);
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setTitle("提示信息");
        mProgressDialog.setMessage("正在下載");
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //執行AsyncTask
                MyTask task = new MyTask();
                task.execute(path);
            }
        });
    }
    /**
     * 使用異步任務的規範
     * 1:聲明一個類繼承自AsyncTask有三個參數
     * 2:第一個參數是要執行的任務,通常是地址,第二個參數是進度,第三個參數是返回值
     * */

    public class MyTask extends AsyncTask<String, Void, Bitmap>{
        //這裏主要完成耗時操作
        @Override
        protected Bitmap doInBackground(String... arg0) {
            //通常使用網絡連接類
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(arg0[0]);
            Bitmap bitmap = null;
            try{
                HttpResponse response = client.execute(get);
                if(response.getStatusLine().getStatusCode() == 200){
                    HttpEntity entity = response.getEntity();
                    byte[] data = EntityUtils.toByteArray(entity);
                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return bitmap;
        }
        //主要完成任務之前的操作
        @Override
        protected void onPreExecute() {
            mProgressDialog.show();
            super.onPreExecute();
        }
        //更新UI
        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            mImageView.setImageBitmap(result);
            mProgressDialog.dismiss();
        }
    }

結果圖:
這裏寫圖片描述


這裏寫圖片描述

發佈了111 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章