【问题标题】:How to add progress bar with percentage while downloading multiple file at a time如何在一次下载多个文件时添加百分比进度条
【发布时间】:2018-12-24 10:46:16
【问题描述】:
  1. 我正在从服务器下载文件。代码如下
  2. 点击下载后,我有图像和声音,以及服务器中的文本文件,因此它将获取所有数据(多个文件)
  3. 由于我的波纹管代码在下载数据时显示百分比和计数对话框,但问题是百分比对话框显示每个数据长度,但我需要对话框显示所有内容的百分比,而不仅仅是一个内容

        private void downloadFiles(String pMain, String pFileName String pURL) {
        Log.i(TAG, "Coming to this downloadBookDetails ");
        int len;
        try {
            URL url = new URL(pURL);
            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();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
            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) / lenghtOfFile));
                Log.i(TAG, "Progress: Bar with Count " + (int) ((total * 100) / lenghtOfFile));
                outStream.write(buff, 0, len);
            }
            outStream.flush();
            outStream.close();
            inStream.close();
        } catch (Exception e) {
            //Add Network Error.
            Log.i(TAG, "Download Error Exception " + e.getMessage());
    
            e.printStackTrace();
        }
    }
    

【问题讨论】:

  • 搜索下载管理器

标签: android


【解决方案1】:

你试过这个Download multiple files with one progressbar java / Android

我无法向问题添加评论,因此添加为答案....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多