本文主要是介绍异步操作AsyncTask(一)从网络下载图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先先声明访问网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.asktaskdownload"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="14"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".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>
布局文件
<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="com.example.asktaskdownload.MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="29dp"android:text="下载网络图片" /><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/ic_launcher" /></RelativeLayout>
package com.example.asktaskdownload;import org.apache.http.HttpEntity;
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.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;public class MainActivity extends Activity {private ImageView imageView;private Button button;//图片的网络地址private String image_path = "http://pic4.nipic.com/20090828/2366808_092035042961_2.jpg";private ProgressDialog dialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.imageView1);button = (Button) findViewById(R.id.button1);dialog = new ProgressDialog(this);dialog.setTitle("提示");dialog.setMessage("正在下载,请稍后...");button.setOnClickListener(new OnClickListener() {//Android3以后不允许在UI主线程访问网络@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew MyTask().execute(image_path);}});}/** 第一个参数:表示要执行的任务的参数,一般传递一个路径* 第二个参数:进度的刻度* 第三个参数:任务执行的返回结果*/public class MyTask extends AsyncTask<String, Void, Bitmap>{//任务执行之前的操作@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();dialog.show();}//完成下载或耗时任务操作@Overrideprotected Bitmap doInBackground(String... params) {HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(params[0]);Bitmap bitmap = null;try {HttpResponse httpResponse = httpClient.execute(httpGet);//返回的状态码为200表示操作成功if(httpResponse.getStatusLine().getStatusCode() == 200){//获取实体类HttpEntity httpEntity = httpResponse.getEntity();//将实体类转换成字节数组byte[] data = EntityUtils.toByteArray(httpEntity);bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);}}catch (Exception e) {e.printStackTrace();}return bitmap;}//主要更新UI操作@Overrideprotected void onPostExecute(Bitmap result) {// TODO Auto-generated method stubsuper.onPostExecute(result);imageView.setImageBitmap(result);dialog.dismiss();}}
}
主页面,点击下载按钮
最后下载完成的结果
这篇关于异步操作AsyncTask(一)从网络下载图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!