android - 如何在一次下载多个文件时添加带百分比的进度条

标签 android

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

最佳答案

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

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

关于android - 如何在一次下载多个文件时添加带百分比的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912354/

相关文章:

javascript - React-Native 如何查看导入文件的变化?

java - 更改 Fragment 方向布局时 addView() 中出现错误

android - Google Play 管理中心 - 停用的 APK 仍显示为 'active'

android - 报告非 fatal error 时如何添加用户名?

android - 在 android 中使用计时器每 10 分钟跟踪一次 Gps

java - 如何使用 datepicker dialogdragment 不同的 edittext

android - 在 window.open 上获取 ERR_CACHE_MISS

android - 如何修复 Kotlin JobCancellationException?

android - 如何在单个 View 中实现两个 fragment ?

java - Android - 致命异常 : main - Unable to start activity ComponentInfo