If you download an image, you have to rewrite the Http protocol once. It is too troublesome to start multiple threads and transfer the handler information. We can encapsulate a tool class directly, so that we can call it at any time during future development.
(1) Add permissions to the list file
(2) editing tool
Package com. example. g05_handler; import java. io. IOException; import org. apache. http. httpResponse; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. util. entityUtils; import android. annotation. suppressLint; import android. app. progressDialog; Import android. content. context; import android. OS. handler; import android. OS. message; import android. util. log; public class DownLoad {private ProgressDialog dialog; public DownLoad (Context context) {// TODO Auto-generated constructor stubdialog = new ProgressDialog (context); dialog. setTitle ("prompt"); dialog. setMessage ("loading") ;}@ SuppressLint ("HandlerLeak") public void Down (final String path, final DownLoadC Allback callback) {final Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubsuper. handleMessage (msg); byte [] result = (byte []) msg. obj; callback. download (result); if (msg. what = 1) {dialog. dismiss () ;}}; class MyThread implements Runnable {@ Overridepublic void run () {// TODO Auto-generated method stubHttpClient client = new DefaultHttpC Lient (); HttpGet httpGet = new HttpGet (path); try {HttpResponse httpResponse = client.exe cute (httpGet); Log. I ("TAG", "------>" + httpResponse. getStatusLine (). getStatusCode (); if (httpResponse. getStatusLine (). getStatusCode () = 200) {byte [] result = EntityUtils. toByteArray (httpResponse. getEntity (); Message message = Message. obtain (); message. obj = result; message. what = 1; handler. sendMessage (message );}} Catch (ClientProtocolException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (client! = Null) {client. getConnectionManager (). shutdown () ;}};} new Thread (new MyThread ()). start (); dialog. show ();} public interface DownLoadCallback {public void download (byte [] data );}}
(3) call this tool class
package com.example.g05_handler;import com.example.g05_handler.DownLoad.DownLoadCallback;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { private Button button; private ImageView imageView; private final String path="http://avatar.csdn.net/D/7/5/1_u013900875.jpg";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)this.findViewById(R.id.button1);imageView=(ImageView)this.findViewById(R.id.imageView1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubDownLoad downLoad=new DownLoad(MainActivity.this);downLoad.Down(path, new DownLoadCallback() {@Overridepublic void download(byte[] data) {// TODO Auto-generated method stubBitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);imageView.setImageBitmap(bitmap);}});}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}