1

I try to download a file using services in android... I was able to write the program and I can download what ever I want. but the problem is with the progress bar!!! I'm not able to define the total file length (or size) and the current downloaded size. I used this code to get the file lenght

            URL url = new URL(urlToDownload);
            URLConnection connection = url.openConnection();
            connection.connect();
            int fileLength = connection.getContentLength();

and I used this part of code to define the current downloaded size

           InputStream input = new BufferedInputStream(connection.getInputStream());
           OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk");

           byte data[] = new byte[1024];
           long total = 0;
           int count;
           while ((count = input.read(data)) != -1) {
               total += count;
               int currentValue=(total * 100 / fileLength);
               output.write(data, 0, count);
           }

the problem is that at the end of download I got something like 241% instead of 100% (because the fileLength for ex was around 12226 and the total was 29349) Do you have any idea about this topic.

Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62
user3429578
  • 1,093
  • 3
  • 12
  • 21
  • could you please share your code how you are showing progress and also verify does the downloading url supports `getContentLength` – Mohammed Sufian Mar 03 '18 at 18:58