现在的位置: 首页 > 综合 > 正文

Android文件下载进度条的实现

2014年11月28日 ⁄ 综合 ⁄ 共 3524字 ⁄ 字号 评论关闭
一个网上的文件,保存到android,并且下载时,会显示一个进度条

  1 页面结构,提供一个button,点BUTTON时,去下载一个图片,main.xml设计如下:

  < ?xml version="1.0" encoding="utf-8"?>

  < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  < TextView

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="@string/hello"

  />

  < Button

  android:text="Start long running task.."

  android:id="@+id/startBtn"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content">

  < /Button>

  < /LinearLayout>

  2 主程序

  package com.liao;

  import java.io.BufferedInputStream;

  import java.io.FileOutputStream;

  import java.io.InputStream;

  import java.io.OutputStream;

  import java.net.URL;

  import java.net.URLConnection;

  import android.app.Activity;

  import android.app.Dialog;

  import android.app.ProgressDialog;

  import android.os.AsyncTask;

  import android.os.Bundle;

  import android.util.Log;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  public class AndroAsync extends Activity {

  public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

  private Button startBtn;

  private ProgressDialog mProgressDialog;

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  startBtn = (Button)findViewById(R.id.startBtn);

  startBtn.setOnClickListener(new OnClickListener(){

  public void onClick(View v) {

  startDownload();

  }

  });

  }

  private void startDownload() {

  String url = "http://image-

  7.verycd.com/0d7596f5db7df1742236466dc91311b1435702(120x120)/thumb.jpg";

  new DownloadFileAsync().execute(url);

  }

  @Override

  protected Dialog onCreateDialog(int id) {

  switch (id) {

  case DIALOG_DOWNLOAD_PROGRESS:

  mProgressDialog = new ProgressDialog(this);

  mProgressDialog.setMessage("Downloading file..");

  mProgressDialog.setProgressStyle

  (ProgressDialog.STYLE_HORIZONTAL);

  mProgressDialog.setCancelable(false);

  mProgressDialog.show();

  return mProgressDialog;

  default:

  return null;

  }

  }

  class DownloadFileAsync extends AsyncTask< String, String, String> {

  @Override

  protected void onPreExecute() {

  super.onPreExecute();

  showDialog(DIALOG_DOWNLOAD_PROGRESS);

  }

  @Override

  protected String doInBackground(String... aurl) {

  int count;

  try {

  URL url = new URL(aurl[0]);

  URLConnection conexion = url.openConnection();

  conexion.connect();

  int lenghtOfFile = conexion.getContentLength();

  Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

  InputStream input = new BufferedInputStream(url.openStream());

  OutputStream output = new FileOutputStream

  ("/sdcard/picture.jpg");

  byte data[] = new byte[1024];

  long total = 0;

  while ((count = input.read(data)) != -1) {

 total += count;

  publishProgress(""+(int)((total*100)/lenghtOfFile));

  output.write(data, 0, count);

  }

  output.flush();

  output.close();

  input.close();

  } catch (Exception e) {

  Log.e("error",e.getMessage().toString());

  System.out.println(e.getMessage().toString());

  }

  return null;

  }

  protected void onProgressUpdate(String... progress) {

  Log.d("ANDRO_ASYNC",progress[0]);

  mProgressDialog.setProgress(Integer.parseInt(progress[0]));

  }

  @Override

  protected void onPostExecute(String unused) {

  dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

  }

  }

  }

  注意这里,在startdownload 方法中,调用了DownloadFileAsync内部类,这个内部类充

  分利用了AsyncTask的异步工作特性,是很方便的,其中注意doInBackground(这个阶段下

  载图片),onProgressUpdate是让进度条完成后消失。

  最后,要记得在androidmanifest.xml中的< /application>后加入

  < uses-permission android:name="android.permission.INTERNET" />

  让能有访问互联网的能力

抱歉!评论已关闭.