在 Android 中实现文件下载的进度条 [英] Implement Progress Bar for File Download in Android

查看:32
本文介绍了在 Android 中实现文件下载的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发的新手.

I am new to Android Development.

我编写了以下代码来从互联网.它工作正常.现在我想在此期间附加一个进度条下载过程.我尝试将 AsyncTask 子类化并在 doInBackground() 方法中编写下载代码.但是,不知怎么的,我想不通.

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 savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       tv = (TextView) findViewById(R.id.TextView01);
       if(checkExternalMedia()==true) {
               DownloadFromUrl(videoURL,fileName);
               tv.append("

Download Complete!");
       }
       else {
               tv.append("

External 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("

External 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("
External 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())));
   }
}

推荐答案

检查:

http://developer.android.com/guide/topics/ui/对话框.html

请参阅在第二个线程中创建进度条的示例.

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

这篇关于在 Android 中实现文件下载的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆