82 Android Handler 下载网络图片
2014-01-19 14:23
281 查看
activity_main.xml ( 布局文件)
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 (主代码)
<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; } }
相关文章推荐
- Android异步下载网络图片(其一:Handler)
- Android异步下载网络图片(其一:Handler)
- Android异步下载网络图片(其一Handler)
- Android网络下载图片Handler处理机制
- android下载网络图片并缓存
- android 通过http网络下载图片 并
- Android 网络下载图片
- android-HttpURLConnection+Handler+Thread下载图片并显示
- Android显示从网络下载图片偏小的问题
- android异步从网络下载图片,并缓存到本地
- 安卓多线程编程系列3:使用handler和message下载网络图片
- android异步下载网络图片(其二)
- android下载网络图片并缓存
- android--AsyncTask的应用(1)网络上图片的下载
- Android异步下载网络图片
- Android 下载网络图片保存到本地
- Android开发之异步下载网络图片并显示到UI界面
- Android 通过开源框架实现加载网络图片并下载到SD卡通知系统相册显示,(并实现分享图片功能)
- android 网络下载图片,加载Bitmap 不oo控件
- Android异步下载网络图片(其二)