84 Android Hnadler 封装下载图片工具类

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="42dp"
        android:layout_marginRight="84dp"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="117dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


DownLoadContent.java

package com.example.android_handler_callback;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

/**
 * 工具类
 * @author Administrator
 *
 */
public class DownLoadContent {

	
	private ProgressDialog dialog;
	public DownLoadContent( Context context) {
		// TODO Auto-generated constructor stub
		dialog=new ProgressDialog(context);
		dialog.setTitle("提示");
		dialog.setMessage("正在获取网络数据...");
	}
	
	public void downLoad(final String path,final DownLoadCallBack callBack){
		
		final Handler handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				byte[] result=(byte[]) msg.obj;
				callBack.LoadContetn(result);
				if(msg.what==1)
				{
					dialog.dismiss();
				}
			}
		};
		
//		 @SuppressWarnings("unused")
		class MyThread implements Runnable{

			@Override
			public void run() {
				// TODO Auto-generated method stub
				HttpClient httpClient=new DefaultHttpClient();
				HttpPost httpPost=new HttpPost(path);
				try {
					HttpResponse response=httpClient.execute(httpPost);
					if(response.getStatusLine().getStatusCode()==200)
					{
						byte[] result=EntityUtils.toByteArray(response.getEntity());
						Message message=new Message();
						message.obj=result;
						message.what=1;
						handler.sendMessage(message);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}finally{
					httpClient.getConnectionManager().shutdown();
				}
			}
			
		} 
		 
		 //启动线程
		 new Thread(new MyThread()).start();
		 dialog.show();
		
	}
	public interface DownLoadCallBack{
		public void LoadContetn(byte[] result);
	}

}


MainActivity.java

package com.example.android_handler_callback;

import com.example.android_handler_callback.DownLoadContent.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 final String img_path="http://www.baidu.com/img/bdlogo.gif";
	private Button button;
	private ImageView imageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)findViewById(R.id.button1);
		imageView=(ImageView)this.findViewById(R.id.imageView1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DownLoadContent downLoadContent=new DownLoadContent(MainActivity.this);
				downLoadContent.downLoad(img_path, new DownLoadCallBack() {
					
					@Override
					public void LoadContetn(byte[] result) {
						// TODO Auto-generated method stub
						Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
						imageView.setImageBitmap(bitmap);
					}
				});
			}
		});
		
	}

	@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;
	}

}


AndroidManifest.xml 在清单文件中添加网络权限








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值