android下圖片

 如果每下載一張圖片,就得重寫一次Http協議,多線程的啓動和handler的信息傳遞就顯得太麻煩了,我們直接來封裝一個工具類,便於我們以後在開發時隨時可以調用。

 (1)在清單文件添加權限

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

(2)編輯工具類
  1. package com.example.g05_handler;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.client.ClientProtocolException;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpGet;  
  9. import org.apache.http.impl.client.DefaultHttpClient;  
  10. import org.apache.http.util.EntityUtils;  
  11.   
  12. import android.annotation.SuppressLint;  
  13. import android.app.ProgressDialog;  
  14. import android.content.Context;  
  15. import android.os.Handler;  
  16. import android.os.Message;  
  17. import android.util.Log;  
  18.   
  19. public class DownLoad {  
  20.   
  21.     private ProgressDialog dialog;  
  22.   
  23.     public DownLoad(Context context) {  
  24.         // TODO Auto-generated constructor stub  
  25.         dialog = new ProgressDialog(context);  
  26.         dialog.setTitle("提示");  
  27.         dialog.setMessage("玩命加載中");  
  28.   
  29.     }  
  30.   
  31.     @SuppressLint("HandlerLeak")  
  32.     public void Down(final String path, final DownLoadCallback callback) {  
  33.         final Handler handler = new Handler() {  
  34.             @Override  
  35.             public void handleMessage(Message msg) {  
  36.                 // TODO Auto-generated method stub  
  37.                 super.handleMessage(msg);  
  38.                 byte[] result = (byte[]) msg.obj;  
  39.   
  40.                 callback.download(result);  
  41.   
  42.                 if (msg.what == 1) {  
  43.                     dialog.dismiss();  
  44.                 }  
  45.             }  
  46.         };  
  47.         class MyThread implements Runnable {  
  48.             @Override  
  49.             public void run() {  
  50.                 // TODO Auto-generated method stub  
  51.                 HttpClient client = new DefaultHttpClient();  
  52.                 HttpGet httpGet = new HttpGet(path);  
  53.                 try {  
  54.                     HttpResponse httpResponse = client.execute(httpGet);  
  55.                     Log.i("TAG""------>"  
  56.                             + httpResponse.getStatusLine().getStatusCode());  
  57.                     if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  58.                         byte[] result = EntityUtils.toByteArray(httpResponse  
  59.                                 .getEntity());  
  60.                         Message message = Message.obtain();  
  61.                         message.obj = result;  
  62.                         message.what = 1;  
  63.                         handler.sendMessage(message);  
  64.                     }  
  65.                 } catch (ClientProtocolException e) {  
  66.                     // TODO Auto-generated catch block  
  67.                     e.printStackTrace();  
  68.                 } catch (IOException e) {  
  69.                     // TODO Auto-generated catch block  
  70.                     e.printStackTrace();  
  71.                 } finally {  
  72.                     if (client != null) {  
  73.                         client.getConnectionManager().shutdown();  
  74.                     }  
  75.                 }  
  76.             };  
  77.         }  
  78.         new Thread(new MyThread()).start();  
  79.         dialog.show();  
  80.     }  
  81.   
  82.     public interface DownLoadCallback {  
  83.         public void download(byte[] data);  
  84.     }  
  85. }  

    (3)調用該工具類
  1. package com.example.g05_handler;  
  2.   
  3. import com.example.g05_handler.DownLoad.DownLoadCallback;  
  4.   
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.ImageView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.     private Button button;  
  16.     private ImageView imageView;  
  17.     private final String path="http://avatar.csdn.net/D/7/5/1_u013900875.jpg";  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         button=(Button)this.findViewById(R.id.button1);  
  23.         imageView=(ImageView)this.findViewById(R.id.imageView1);  
  24.         button.setOnClickListener(new View.OnClickListener() {  
  25.               
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 // TODO Auto-generated method stub  
  29.                 DownLoad downLoad=new DownLoad(MainActivity.this);  
  30.                 downLoad.Down(path, new DownLoadCallback() {  
  31.                       
  32.                     @Override  
  33.                     public void download(byte[] data) {  
  34.                         // TODO Auto-generated method stub  
  35.                         Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);  
  36.                         imageView.setImageBitmap(bitmap);  
  37.                     }  
  38.                 });  
  39.             }  
  40.         });  
  41.     }  
  42.   
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return true;  
  48.     }  
  49.   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章