开发者

Implement Progress Bar for File Download in Android

I am new to Android Development.

I've written the following code to download a video file from the internet. It works fine. Now I want to attach a progress bar during the download process.I tried to subclass AsyncTask and write the download code inside doInBackground() method. But, somehow I can't figure it out.

Can somebody please help me modify this code to accomplish that?


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 saved开发者_开发百科InstanceState) {
       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())));
   }
}


Check:

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

See the example on creating a progress bar within a second thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜