Android测试AsyncTask下载图片

  1 package com.example.myact8_async;
  2 
  3 import org.apache.http.HttpEntity;
  4 import org.apache.http.HttpResponse;
  5 import org.apache.http.client.HttpClient;
  6 import org.apache.http.client.methods.HttpGet;
  7 import org.apache.http.impl.client.DefaultHttpClient;
  8 import org.apache.http.util.EntityUtils;
  9 import android.app.ProgressDialog;
 10 import android.graphics.Bitmap;
 11 import android.graphics.BitmapFactory;
 12 import android.os.AsyncTask;
 13 import android.os.Bundle;
 14 import android.support.v7.app.ActionBarActivity;
 15 import android.view.Menu;
 16 import android.view.MenuItem;
 17 import android.view.View;
 18 import android.view.View.OnClickListener;
 19 import android.widget.Button;
 20 import android.widget.ImageView;
 21 /**
 22  * 测试AsyncTask异步任务进行下载图片
 23  * @Describe: 
 24  * @package: com.example.myact8_async
 25  * @author shaobn
 26  * @date 2015-9-11 下午4:03:24
 27  */
 28 public class MainActivity extends ActionBarActivity {
 29     private Button button;
 30     private ImageView iv;
 31     private ProgressDialog pd;
 32     private final static String URLPATH = "http://d.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec919c6f1960d0f703918fc109.jpg";
 33     @Override
 34     protected void onCreate(Bundle savedInstanceState) {
 35         super.onCreate(savedInstanceState);
 36         setContentView(R.layout.activity_main);
 37         iv = (ImageView) this.findViewById(R.id.imageView1);
 38         pd = new ProgressDialog(this);
 39         pd.setTitle("下载提示");
 40         pd.setMessage("正在下载,请稍后...");
 41         button = (Button) this.findViewById(R.id.button1);
 42         button.setOnClickListener(new OnClickListener() {
 43             
 44             @Override
 45             public void onClick(View arg0) {
 46                 // TODO Auto-generated method stub
 47                 new MyAsync().execute(MainActivity.URLPATH);
 48             }
 49         });
 50     }
 51     /**
 52      * 自定义异步任务类
 53      * @Describe: 
 54      * @package: com.example.myact8_async
 55      * @author shaobn
 56      * @date 2015-9-11 下午4:27:09
 57      */
 58     public class MyAsync extends AsyncTask<String, Void, Bitmap>{
 59         //下载进程
 60         @Override
 61         protected Bitmap doInBackground(String... arg0) {
 62             // TODO Auto-generated method stub
 63             HttpClient hClient = new DefaultHttpClient();
 64             HttpGet hGet = new HttpGet(MainActivity.URLPATH);
 65             Bitmap bitmap = null;
 66             try {
 67                 //实现图片下载
 68                 HttpResponse hResponse = hClient.execute(hGet);
 69                 if(hResponse.getStatusLine().getStatusCode()==200){
 70                     HttpEntity hEntity = hResponse.getEntity();
 71                     byte[] byt = EntityUtils.toByteArray(hEntity);
 72                     bitmap = BitmapFactory.decodeByteArray(byt, 0, byt.length);
 73                 }
 74             } catch (Exception e) {
 75                 // TODO: handle exception
 76                 e.printStackTrace();
 77             }
 78             return bitmap;
 79             
 80         }
 81         //下载后执行操作
 82         @Override
 83         protected void onPostExecute(Bitmap result) {
 84             // TODO Auto-generated method stub
 85             super.onPostExecute(result);
 86             MainActivity.this.iv.setImageBitmap(result);
 87             MainActivity.this.pd.dismiss();
 88         }
 89         //下载前动作
 90         @Override
 91         protected void onPreExecute() {
 92             // TODO Auto-generated method stub
 93             super.onPreExecute();
 94             MainActivity.this.pd.show();
 95         }        
 96         
 97     }
 98 
 99     @Override
100     public boolean onCreateOptionsMenu(Menu menu) {
101         // Inflate the menu; this adds items to the action bar if it is present.
102         getMenuInflater().inflate(R.menu.main, menu);
103         return true;
104     }
105 
106     @Override
107     public boolean onOptionsItemSelected(MenuItem item) {
108         // Handle action bar item clicks here. The action bar will
109         // automatically handle clicks on the Home/Up button, so long
110         // as you specify a parent activity in AndroidManifest.xml.
111         int id = item.getItemId();
112         if (id == R.id.action_settings) {
113             return true;
114         }
115         return super.onOptionsItemSelected(item);
116     }
117 }

自己写的小程序,方便保存一下。

posted @ 2015-09-11 16:31  邻家小书童  阅读(210)  评论(0编辑  收藏  举报