|
/********************************************************
*2015年3月2日 11:18:37
*功能:通过多线程从互联网现在一幅图片并显示、
*知识点:
* Runnable多线程类
* 图片的解码:BitmapFactory.decodeByteArray(data,0,data.length);
*
*step:1 Runnable多线程类run方法中写多线程函数并通过Message类传给Handler
* 2 Handler中过public void handleMessage(Message msg)获取多线程中传回的数据
* 3 onClick()通过new Thread(new MyThread()).start();开启线程
*/
package com.example.handler;
import java.io.InputStream;
import org.apache.http.HttpResponse;
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.R.string;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
private ImageView imagineView;
private String image_path="http://www.51hei.com/bbs/static/image/common/logo.png";
private final int IS_FINISH=1;
private Handler handler=new Handler(){ // step3:获取数据
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method
byte[] data=(byte[])msg.obj;//取书据
Bitmap bm=BitmapFactory.decodeByteArray(data,0,data.length);//图片解码
imagineView.setImageBitmap(bm);
if(msg.what==IS_FINISH)
{
Toast.makeText(MainActivity.this,"下载成功",Toast.LENGTH_SHORT).show();
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)this.findViewById(R.id.button1);
imagineView=(ImageView)this.findViewById(R.id.imageView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new Thread(new MyThread()).start(); //step4:开线程
}
});
}
public class MyThread implements Runnable //step1:定义多线程类
{
@Override
public void run() { // step2:线程耗时,完成下载
// TODO Auto-generated method stub
HttpClient httpClient=new DefaultHttpClient();//获取网络数据
HttpGet httpGet=new HttpGet(image_path);
HttpResponse httpResponse=null;
//InputStream inputStream=null;
try{
httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200)
{
//inputStream=httpResponse.getEntity().getContent();
byte[]data=EntityUtils.toByteArray(httpResponse.getEntity());
//把数据发送给Handler
//Message可以携带数据返回给Handler
Message message=Message.obtain();//携带数据
message.obj=data;
message.what=IS_FINISH;
handler.sendMessage(message);//发送到handler
}
}catch(Exception e)
{
Toast.makeText(MainActivity.this,"捕获异常信息1",Toast.LENGTH_SHORT).show();
}
}
}
@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;
}
}
|
|