|
/********************************************************
*2015年3月2日 11:18:37
*功能:通過(guò)多線程從互聯(lián)網(wǎng)現(xiàn)在一幅圖片并顯示、
*知識(shí)點(diǎn):
* Runnable多線程類
* 圖片的解碼:BitmapFactory.decodeByteArray(data,0,data.length);
*
*step:1 Runnable多線程類run方法中寫(xiě)多線程函數(shù)并通過(guò)Message類傳給Handler
* 2 Handler中過(guò)public void handleMessage(Message msg)獲取多線程中傳回的數(shù)據(jù)
* 3 onClick()通過(guò)new Thread(new MyThread()).start();開(kāi)啟線程
*/
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.zg4o1577.cn/bbs/static/image/common/logo.png";
private final int IS_FINISH=1;
private Handler handler=new Handler(){ // step3:獲取數(shù)據(jù)
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method
byte[] data=(byte[])msg.obj;//取書(shū)據(jù)
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:開(kāi)線程
}
});
}
public class MyThread implements Runnable //step1:定義多線程類
{
@Override
public void run() { // step2:線程耗時(shí),完成下載
// TODO Auto-generated method stub
HttpClient httpClient=new DefaultHttpClient();//獲取網(wǎng)絡(luò)數(shù)據(jù)
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());
//把數(shù)據(jù)發(fā)送給Handler
//Message可以攜帶數(shù)據(jù)返回給Handler
Message message=Message.obtain();//攜帶數(shù)據(jù)
message.obj=data;
message.what=IS_FINISH;
handler.sendMessage(message);//發(fā)送到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;
}
}
|
|