Handler
不想多说
private Button btn;
private ImageView image;
private ProgressDialog dialog;
private static int FINSH=1;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==FINSH){
dialog.dismiss();
}
byte[] data=(byte[])msg.obj;
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
image.setImageBitmap(bitmap);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog=new ProgressDialog(this);
dialog.setMessage("load.....");
btn=(Button) findViewById(R.id.button1);
image=(ImageView) findViewById(R.id.imageView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Down()).start();
dialog.show();
}
});
}
public class Down implements Runnable{
private String path="http://www.baidu.com/img/315-270_e9a7cf5b20f0b58d37dcbe8b9b170713.gif";
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(path);
try {
HttpResponse response=client.execute(post);
if(response.getStatusLine().getStatusCode()==200){
byte[] bs=EntityUtils.toByteArray(response.getEntity());
Message message=Message.obtain();
message.obj=bs;
message.what=FINSH;
handler.sendMessage(message);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
client.getConnectionManager().shutdown();
}
}
}