您的位置:首页 > 移动开发 > Android开发

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

2014-01-19 16:03 453 查看
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 在清单文件中添加网络权限






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: