在Android中实现文件下载的进度条

时间:2011-02-18 13:41:58

标签: android android-asynctask

我是Android开发的新手。

我已经编写了以下代码来从中下载视频文件 互联网。它工作正常。现在我想要附上一个进度条 下载过程。我试图将AsyncTask子类化,并在doInBackground()方法中编写下载代码。但是,不知怎的,我无法弄明白。

有人可以帮我修改一下这段代码吗?


package sample.android.download;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class DownloadDemo extends Activity {

   private TextView tv;

   private String videoURL = "http://mysite-name.com/videos/videofile_name.mp4";
   private String fileName = "my_video.mp4";


   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       tv = (TextView) findViewById(R.id.TextView01);
       if(checkExternalMedia()==true) {
               DownloadFromUrl(videoURL,fileName);
               tv.append("\n\nDownload Complete!");
       }
       else {
               tv.append("\n\nExternal Media is NOT readable/writable");
       }
   }

   /** Method to check whether external media available and writable. */

   private boolean checkExternalMedia(){
       boolean mExternalStorageAvailable = false;
       boolean mExternalStorageWriteable = false;
       boolean stat;
       String state = Environment.getExternalStorageState();

       if (Environment.MEDIA_MOUNTED.equals(state)) {
           // Can read and write the media
           mExternalStorageAvailable = mExternalStorageWriteable = true;
           stat = true;
       } 
       else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
           // Can only read the media
           mExternalStorageAvailable = true;
           mExternalStorageWriteable = false;
           stat = false;
       } else {
           // Can't read or write
           mExternalStorageAvailable = mExternalStorageWriteable = false;
           stat = false;
       }
       tv.append("\n\nExternal Media: readable="+mExternalStorageAvailable+ "writable="+mExternalStorageWriteable);

       return stat;
   }

   /** Method to download an external file from the network to the SD card. */

   public void DownloadFromUrl(String videoURL, String fileName) {

       try {
                   File root = android.os.Environment.getExternalStorageDirectory();
                   tv.append("\nExternal file system root: "+root);

                   File dir = new File (root.getAbsolutePath() + "/video");
                   //dir.mkdirs();

                   URL url = new URL(videoURL); //you can write here any link
                   File file = new File(dir, fileName);

                   long startTime = System.currentTimeMillis();
                   Log.d("ImageManager", "download begining");
                   Log.d("ImageManager", "download url:" + url);
                   Log.d("ImageManager", "downloaded file name:" + fileName);

                   /* Open a connection to that URL. */
                   URLConnection ucon = url.openConnection();

                   /*
                    * Define InputStreams to read from the URLConnection.
                    */
                   InputStream is = ucon.getInputStream();
                   BufferedInputStream bis = new BufferedInputStream(is);

                   /*
                    * Read bytes to the Buffer until there is nothing more to read(-1).
                    */
                   ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                   int current = 0;
                   while ((current = bis.read()) != -1) {
                      baf.append((byte) current);
                   }


                   /* Convert the Bytes read to a String. */
                   FileOutputStream fos = new FileOutputStream(file);
                   fos.write(baf.toByteArray());
                   fos.flush();
                   fos.close();
                   Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

       } catch (IOException e) {
                       Log.d("ImageManager", "Error: " + e);
       }

       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
   }
}

1 个答案:

答案 0 :(得分:1)

检查:

http://developer.android.com/guide/topics/ui/dialogs.html

请参阅第二个帖子中创建进度条的示例。