【问题标题】:Progress count not shows 100%进度计数未显示 100%
【发布时间】:2018-12-26 09:50:38
【问题描述】:

1.如下我的代码我已经更新了。

2. 每当我尝试从服务器下载数据时,我都会使用进度计数对话框,但下载开始时会显示计数百分比。

3. 但问题是计数仅显示高达 30%,当百分比达到 30% 时对话框将被关闭(这意味着所有数据下载成功)

4.但我需要显示对话框最多 100% 完成然后它应该被关闭.. 任何文件都有服务器它大约是 5 到 6 个文件。

@Override
    protected void onPreExecute() {
        this.progressDialog = new ProgressDialog(BaseApplication.getInstance().getCurrentActivity());
        this.progressDialog.setMessage("Downloading Please wait.");
        this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        this.progressDialog.setMax(100);
        this.progressDialog.setCancelable(false);
        this.progressDialog.show();
    }

     @Override
    protected Boolean doInBackground(String... strings) {

     // Here is my downloading code  but i removed 
             DownloadData("" , "" , ""); //stuff
             }


        return true;

    }
      @Override
    protected void onProgressUpdate(String... values) {
        this.progressDialog.incrementProgressBy(5);

    }

     protected void onPostExecute(Boolean result) {

     this.progressDialog.dismiss();
      }

 private void downloadData(String Folder, String Name, String URL) {
        Log.i(TAG, "Coming to this downloadBookDetails ");
//        int url1 = Integer.parseInt(pDownloadURL);
        int len;
        try {


               // int urlsixe = pDownloadURL.length();
               // Log.i(TAG, "URL lenght" + urlsixe);
                URL url = new URL(URL);
                Log.i(TAG, "pDownload URL" + url);
                URLConnection ucon = url.openConnection();
                ucon.setReadTimeout(5000);
                ucon.setConnectTimeout(10000);
                ucon.connect();
                int lenghtOfFile = ucon.getContentLength();
                Log.i(TAG, "lenght of file" + lenghtOfFile);
                InputStream is = ucon.getInputStream();
                Log.i(TAG, " InputStream" + is);
                BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
                Log.i(TAG, " BufferedInputStream" + inStream);
                File directory = new File(pMainFolder, pFileName);
                Log.i(TAG, "File Name dir" + directory);
                FileOutputStream outStream = new FileOutputStream(directory);
                byte[] buff = new byte[5 * 1024];


                long total = 0;
                while ((len = inStream.read(buff)) != -1) {
                    total += len;

                   // publishProgress("" + (int) ((total * 100) / urlsixe));
//                    Log.i(TAG, "Progress: Bar with Count " + (int) ((total * 100) / lenghtOfFile));
                    outStream.write(buff, 0, len);


                }

            publishProgress();
                outStream.flush();
                outStream.close();
                inStream.close();



        } catch (Exception e) {
            //Add Network Error.
            Log.i(TAG, "Download Error Exception " + e.getMessage());

            e.printStackTrace();
        }
    }

【问题讨论】:

  • 我认为 this.progressDialog.incrementProgressBy(5);不应硬编码为 5。它应根据下载的文件量计算百分比。
  • 有什么理由增加 5?
  • 只是测试目的,我把它设为 5,但当我把它设为 1 时它也不起作用
  • publishProgress() 是什么?无论如何,您应该使用有关读取字节数和文件长度的百分比。无论如何,您应该提高输入/输出关闭的鲁棒性。
  • 我试过这个用一个进度条java / Android下载多个文件

标签: java android


【解决方案1】:

在您的 Asyntask onPostExecute 检查是否达到最大值。如果达到则关闭对话框

  protected void onPostExecute(Boolean result) {

   if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {     
    this.progressDialog.dismiss();
     }
  }

【讨论】:

  • 它不起作用。当我尝试添加你的一段代码时,对话框在 50% 到来时停止..
  • 当计数达到 50% 时,我的数据下载成功
【解决方案2】:

首先,在进度条中使用 setProgress(0) 将进度设置为 0

其次,您正在下载文件后更新进度条。它不会像您预期的那样工作。

在你的 while 循环中使用 publishProgress()。您已经在下载方法的开头保存了文件的长度。通过total+(len/lengthOfFile*100)计算文件下载的百分比。现在,使用此信息发送进度条百分比的更新值,而不是硬编码增量 ProgressBy(5)。

【讨论】:

    【解决方案3】:

    你应该通过publistProgress将进度百分比传递给onProgeressUpdate

    试试这个

     while ((len = inStream.read(buff)) != -1) {
                        total += len;
    
                       publishProgress("Downloading.. "+Name+" " + (int) ((total * 100) / inStream.read(buff))));
    //                    Log.i(TAG, "Progress: Bar with Count " + (int) ((total * 100) / lenghtOfFile));
                        outStream.write(buff, 0, len);
    
    
                    }
    
              //  publishProgress();
    
    
      @Override
        protected void onProgressUpdate(String... values) {
            this.progressDialog.incrementProgressBy(values[0]);
    
        }
    

    【讨论】:

    • 我怎样才能通过。 onProgeressUpdate 通过 publistProgress
    • 查看我何时添加您的代码..我在服务器中有 6 个文件,因此进度将显示每个文件下载的百分比
    • 表示当一个文件下载成功后会再次出现对话框
    • 但您只调用了一次 DownloadData("..)
    • 否则你会用类似“正在下载文件1”“正在下载文件2等”的文字显示进度
    猜你喜欢
    • 2011-06-07
    • 2020-03-24
    • 2021-11-03
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多