首页 > 代码库 > Android学习笔记之AsyncTask实现文件下载任务
Android学习笔记之AsyncTask实现文件下载任务
(1)该文件下载主要演示异步任务下载图片
(2)布局代码如下:一个ImageView用于存放下载的图片,Button用于开始异步任务下载
<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" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="141dp" android:layout_marginTop="135dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/imageView1" android:layout_marginBottom="80dp" android:text="网页下载图片" /> </RelativeLayout>
(3)控制类
package com.example.asynctask_download; 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.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.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button button1; private ImageView imageView1; private final String IMAGE_PATH = "http://www.baidu.com/img/bd_logo1.png";// 设置网页图片的地址 private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) this.findViewById(R.id.button1); imageView1 = (ImageView) this.findViewById(R.id.imageView1); /* * 下载进度条的设置 */ progressDialog = new ProgressDialog(this); progressDialog.setTitle("提示"); progressDialog.setCancelable(false); progressDialog.setMessage("正在下载图片,请耐心等候!"); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 异步任务、执行下载图片 */ new MyTask().execute(IMAGE_PATH); } }); } /* * 异步任务:三个参数、四个步骤onPreExecute()、 * doInBackground()、onProgressUpdate()、onPostExecute()、没有参数传传进去的时候可以使用void */ public class MyTask extends AsyncTask<String, Integer, byte[]> { /* * onPreExecute()方法在后台操作开始前运行在UI线程上 */ @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } /* * doInBackground方法运行在后台并处理后台操作 */ @Override protected byte[] doInBackground(String... params) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); byte[] result = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toByteArray(httpResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return result; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } /* * 一旦后台操作完毕,onPostExecute()方法就会在UI线程中运行 */ @Override protected void onPostExecute(byte[] result) { super.onPostExecute(result); /* * 将下载的bitmap放置在imagView中去 * result就是下载的图片 */ Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length); imageView1.setImageBitmap(bitmap); progressDialog.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
结果:
Android学习笔记之AsyncTask实现文件下载任务
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。