在Android文件下载进度条

在Android文件下载进度条

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

问题描述

如何显示剩余KB的文件被下载的进度条中的机器人。例如12KB / 120KB是remaining..then 97KB / 120KB ...等

我们能有这样的进度对话框显示的图像

解决方案

 类DownloadFileAsync扩展的AsyncTask<字符串,字符串,字符串> {
        私人ProgressDialog mProgressDialog;
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            mProgressDialog =新ProgressDialog(UrlTestActivity.this);
            mProgressDialog.setMessage(下载文件...);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(假);
            mProgressDialog.show();
        }

        @覆盖
        保护字符串doInBackground(字符串... aurl){
            诠释计数;
            尝试 {

                的for(int i = 0;我3;;我++){
                    网址URL =新的URL(http://nodeload.github.com/nexes/Android-File-Manager/zipball/master);
                    URLConnection的体conexion = url.openConnection();
                    conexion.connect();
                    INT lenghtOfFile = conexion.getContentLength();
                    InputStream的是= url.openStream();
                    文件testDirectory =新的文件(Environment.getExternalStorageDirectory()+/文件夹);
                    如果(!testDirectory.exists()){
                        testDirectory.mkdir();
                    }
                    FileOutputStream中FOS =新的FileOutputStream(testDirectory +/"+(i+100)+".zip);
                    字节的数据[] =新的字节[1024];
                    总长= 0;
                    INT进度= 0;
                    而((计数= is.​​read(数据))!=  -  1){
                        共有+ =计数;
                        INT progress_temp =(int)的总* 100 / lenghtOfFile;
                        publishProgress(+ progress_temp);
                        如果(progress_temp%10 == 0安培;&安培;进步= progress_temp!){
                            进度= progress_temp;
                        }
                        fos.write(数据,0,计数);
                    }
                    is.close();
                    fos.close();
                }
            }赶上(例外五){}
            返回null;

        }
        保护无效onProgressUpdate(字符串...进度){
             Log.d(ANDRO_ASYNC,进步[0]);
             mProgressDialog.setProgress(的Integer.parseInt(进展[0]));
        }

        @覆盖
        保护无效onPostExecute(字符串使用){
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
 

How to show the remaining KB's of file is to be downloaded in progress bar in android.e.g 12kb/120kb is remaining..then 97kb/120kb...etc

Can we have this progress dialog as shown in the image

解决方案
class DownloadFileAsync extends AsyncTask<String, String, String> {
        private ProgressDialog mProgressDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(UrlTestActivity.this);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }

        @Override
        protected String doInBackground(String... aurl) {
            int count;
            try {

                for (int i = 0; i < 3; i++) {
                    URL url = new URL("http://nodeload.github.com/nexes/Android-File-Manager/zipball/master");
                    URLConnection conexion = url.openConnection();
                    conexion.connect();
                    int lenghtOfFile = conexion.getContentLength();
                    InputStream is = url.openStream();
                    File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder");
                    if (!testDirectory.exists()) {
                        testDirectory.mkdir();
                    }
                    FileOutputStream fos = new FileOutputStream(testDirectory+ "/"+(i+100)+".zip");
                    byte data[] = new byte[1024];
                    long total = 0;
                    int progress = 0;
                    while ((count = is.read(data)) != -1) {
                        total += count;
                        int progress_temp = (int) total * 100 / lenghtOfFile;
                        publishProgress(""+ progress_temp);
                        if (progress_temp % 10 == 0 && progress != progress_temp) {
                            progress = progress_temp;
                        }
                        fos.write(data, 0, count);
                    }
                    is.close();
                    fos.close();
                }
            } catch (Exception e) {}
            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);
        }
    }

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

07-30 22:33