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

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

问题描述

我是新来的Andr​​oid开发。

我已经写了下面的code下载的视频文件 因特网。它工作正常。现在,我想在附加进度条 下载process.I试图继承AsyncTask的写内部doInBackground()方法下载code。但是,不知何故,我不明白。

有人可以请帮我修改此code来实现这一目标?


 包sample.android.download;

进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.net.URL;
进口java.net.URLConnection中;

进口org.apache.http.util.ByteArrayBuffer;

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

公共类涓嬭浇延伸活动{

   私人TextView的电视;

   私人字符串videoURL =htt​​p://mysite-name.com/videos/videofile_name.mp4;
   私人字符串文件名=my_video.mp4;


   / **第一次创建活动时调用。 * /
   @覆盖
   公共无效的onCreate(包savedInstanceState){
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.main);
       电视=(TextView中)findViewById(R.id.TextView01);
       如果(checkExternalMedia()==真){
               DownloadFromUrl(videoURL,文件名);
               tv.append(\ñ\ nDownload完成!);
       }
       其他 {
               tv.append(\ñ\ nExternal媒体不是可读/写);
       }
   }

   / **方法来检查是否外部媒体提供可写的。 * /

   私人布尔checkExternalMedia(){
       布尔mExternalStorageAvailable = FALSE;
       布尔mExternalStorageWriteable = FALSE;
       布尔统计;
       字符串状态= Environment.getExternalStorageState();

       如果(Environment.MEDIA_MOUNTED.equals(州)){
           //可读写的介质
           mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
           STAT = TRUE;
       }
       否则,如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
           //只能读取媒体
           mExternalStorageAvailable = TRUE;
           mExternalStorageWriteable = FALSE;
           STAT = FALSE;
       } 其他 {
           //无法读取或写入
           mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
           STAT = FALSE;
       }
       tv.append(\ñ\ nExternal媒体:可读=+ mExternalStorageAvailable +可写=+ mExternalStorageWriteable);

       返回统计;
   }

   / **方法从网络到SD卡中下载的外部文件。 * /

   公共无效DownloadFromUrl(字符串videoURL,字符串文件名){

       尝试 {
                   文件根= android.os.Environment.getExternalStorageDirectory();
                   tv.append(\ nExternal文件系统的根:+根);

                   文件DIR =新的文件(root.getAbsolutePath()+/视频);
                   //dir.mkdirs();

                   网址URL =新的URL(videoURL); //你可以在这里写任何链接
                   档案文件=新的文件(目录,文件名);

                   长的startTime = System.currentTimeMillis的();
                   Log.d(ImageManager,下载开始时);
                   Log.d(ImageManager,下载网址:+网址);
                   Log.d(ImageManager,已下载文件的名称:+文件名);

                   / *打开到URL的连接。 * /
                   URLConnection的UCON = url.openConnection();

                   / *
                    *定义InputStreams从URLConnection的阅读。
                    * /
                   InputStream的是= ucon.getInputStream();
                   的BufferedInputStream双=新的BufferedInputStream(是);

                   / *
                    *读取字节的缓冲区,直到有没有更多阅读(-1)。
                    * /
                   ByteArrayBuffer BAF =新ByteArrayBuffer(5000);
                   INT电流= 0;
                   而((电流= bis.read())!=  -  1){
                      baf.append((字节)电流);
                   }


                   / *读取转换为字符串的字节数。 * /
                   FileOutputStream中FOS =新的FileOutputStream(文件);
                   fos.write(baf.toByteArray());
                   fos.flush();
                   fos.close();
                   Log.d(ImageManager,下载准备在+((System.currentTimeMillis的() - 的startTime)/ 1000)+秒);

       }赶上(IOException异常E){
                       Log.d(ImageManager,错误:+ E);
       }

       sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory())));
   }
}
 

解决方案

查看:

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

请参阅上第二个线程中创建一个进度条的例子。

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

解决方案

Check:

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

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

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

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