下面举个例子:
在main.xml文件中:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <ImageView 8 android:id="@+id/imageView1" 9 android:layout_width="fill_parent" 10 android:layout_height="0dp" 11 android:layout_weight="7"/> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="fill_parent" 16 android:layout_height="0dp" 17 android:background="@drawable/shape_button" 18 android:layout_weight="1" 19 android:text="下载图片" /> 20 </LinearLayout>
在MainActivity.java文件中:
package com.example.handler1; import java.io.IOException; import org.apache.http.HttpEntity; 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.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private String image_path = "http://p0.so.qhimg.com/t01aa653c8cd80777a5.jpg"; private Message message; private Button button; private ProgressDialog dialog; private ImageView imageView; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO 自动生成的方法存根 super.handleMessage(msg); byte[] data = (byte[]) msg.obj; imageView.setImageBitmap(BitmapFactory.decodeByteArray(data, 0,data.length)); if(1 == msg.what){ dialog.dismiss(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("提示"); dialog.setMessage("正在下载,请稍候..."); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 dialog.show(); dialog.setCancelable(false); new Thread(new MyThread()).start(); } }); imageView = (ImageView) findViewById(R.id.imageView1); } @Override public 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; } private class MyThread implements Runnable{ @Override public void run() { // TODO 自动生成的方法存根 HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(image_path); try { HttpResponse response = client.execute(get); if(response.getStatusLine().getStatusCode() == 200){ HttpEntity entity = response.getEntity(); byte[] data = EntityUtils.toByteArray(entity); message = Message.obtain(); message.obj = data; message.what = 1; handler.sendMessage(message); } } catch (Exception e) { // TODO: handle exception } } } }
在AndroidManifest.xml文件中配置下网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
运行结果: