android从网上加载图片简单示例


package com.example.login;

import java.io.IOException;
import java.io.InputStream;

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.protocol.DefaultedHttpContext;

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;

public class Inter_Activity extends Activity {
	ImageView imageView;
	Button btn_download;
	// 憨豆
	Handler handler;
	// 进度条
	ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inter_picture);
		btn_download = (Button) findViewById(R.id.bt_download_pic);
		progressDialog = new ProgressDialog(this);
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				// super.handleMessage(msg);
				//移除
				progressDialog.dismiss();
				if (msg.what == 1) {
					// 下载成功
					Bitmap bitmap = (Bitmap) msg.obj;
					imageView.setImageBitmap(bitmap);
				} else {
					Toast.makeText(Inter_Activity.this, "网络不给力", 2000).show();
				}
			}
		};
		imageView = (ImageView) findViewById(R.id.iv_download_pic);
		btn_download = (Button) findViewById(R.id.bt_download_pic);

		btn_download.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				progressDialog.setTitle("图片正在下载");
				progressDialog.setMessage("正在玩命加载中...");
				progressDialog.setCancelable(false);
				progressDialog.show();
				// 下载
				downloadThread dtThread = new downloadThread();
				dtThread.start();
			}
		});
	}

	class downloadThread extends Thread {
		@Override
		public void run() {
			Message message = new Message();
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(
					"http://pica.nipic.com/2007-11-09/2007119124413448_2.jpg");
			try {
				HttpResponse response = client.execute(get);
				if (response.getStatusLine().getStatusCode() == 200) {
					InputStream inputStream = response.getEntity().getContent();
					Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
					// imageView.setImageBitmap(bitmap);
					message.what = 1;
					message.obj = bitmap;
					handler.sendMessage(message);
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				message.what = 2;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				message.what = 2;
			}
		}
	}
}
布局文件:



    

    


你可能感兴趣的:(网上加载图片,android,android,图片,imageview,class)