您的位置:首页 > 理论基础 > 计算机网络

82 Android Handler 下载网络图片

2014-01-19 14:23 281 查看
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_centerHorizontal="true"
android:layout_marginBottom="57dp"
android:text="Button" />

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

</RelativeLayout>


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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_handle_hello"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 添加网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_handle_hello.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


MainActivity.java (主代码)

package com.example.android_handle_hello;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
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 Button button;
private ImageView imageView;
private final String imag_paht="http://www.baidu.com/img/bdlogo.gif";
private final int IS_COMPLETE=1;//表示完成
private  Handler handler=new Handler(){
public void handleMessage(Message msg) {
byte[] data=(byte[])msg.obj;
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
if(msg.what==IS_COMPLETE)
{
dialog.dismiss();
}
};
};

private ProgressDialog dialog;
@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);
dialog=new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("正在获取数据");

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new DownLoadImage()).start();
dialog.show();
}
});
}

public class DownLoadImage implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
//完成耗时操作
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(imag_paht);
try {
HttpResponse response=httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
byte[] result=EntityUtils.toByteArray(response.getEntity());
/*
* Return a new Message instance from the global pool.
*  Allows us to avoid allocating new objects in many cases.
*/
Message message=Message.obtain();
message.obj=result;
message.what=IS_COMPLETE;
handler.sendMessage(message);

}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(httpClient != null)
{
try {
httpClient.getConnectionManager().shutdown();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}

}

}

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

}


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