佈局文件, 一個Button開啓點擊事件, 一個ImageView用來顯示所下載的圖片java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
而後是Activity頁面代碼android
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.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.example.activity001.R; public class DownloadImageActivity extends Activity { private Button button; private ImageView imageView; private final static String IMAGE_URL = "http://d.hiphotos.bdimg.com/album/s%3D1000%3Bq%3D90/sign=00478c8fc9ea15ce45eee40986300182/dcc451da81cb39dbb581855cd0160924ab183072.jpg"; private ProgressDialog progressDialog = null; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg != null) { byte[] data = (byte[]) msg.obj; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bitmap); if(msg.what == 1) { progressDialog.dismiss(); Toast.makeText(DownloadImageActivity.this, "下載完成", Toast.LENGTH_SHORT).show(); button.setVisibility(View.INVISIBLE); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download_image); button = (Button) findViewById(R.id.button); button.setText("點擊下載圖片"); imageView = (ImageView) findViewById(R.id.imageView); progressDialog = new ProgressDialog(this); progressDialog.setTitle("提示"); progressDialog.setMessage("正在下載, 請等待..."); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); new Thread(new MyThread()).start(); } }); } private class MyThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(IMAGE_URL); try { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } HttpResponse resp = httpClient.execute(httpGet); int respCode = resp.getStatusLine().getStatusCode(); if(respCode == 200) { byte[] date = EntityUtils.toByteArray(resp.getEntity()); Message msg = Message.obtain(); msg.obj = date; msg.what = 1; handler.sendMessage(msg); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }
運行效果圖:apache