老罗Android(19)AsyncTask下载图片例子

来源:互联网 发布:青岛知行国际派遣公司 编辑:程序博客网 时间:2024/06/10 12:25

布局文件:

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

结果图:
这里写图片描述


这里写图片描述

0 0
原创粉丝点击