title: Show dialog with progress as in percentage
body: <p>I am downloading some files from the internet using the async task. For this matter I know it is very relevant to show to the user the progress so far. All my implementation of downloading the file is very successful, but the only problem is with the progress dialog, this shows a 0 percent even when the download command has been started.</p>
  
  <p>This is how i do it</p>
  
  <pre><code> // Show Dialog Box with Progress bar
    @Override
    protected Dialog onCreateDialog(int id) {
      switch (id) {
          case progress_bar_type:
              prgDialog = new ProgressDialog(this);
              prgDialog.setMessage("Downloading Mp3 file. Please wait...");
              prgDialog.setIndeterminate(false);
              prgDialog.setMax(100);
              prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
              prgDialog.setCancelable(false);
              prgDialog.show();
              return prgDialog;
          default:
              return null;
      }
  }
  
  
  
  // Async Task Class
  class DownloadMusicfromInternet extends AsyncTask&lt;String, String, String&gt; {
  
      // Show Progress bar before downloading Music
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          // Shows Progress Bar Dialog and then call doInBackground method
          showDialog(progress_bar_type);
      }
  
      // Download Music File from Internet
      @Override
      protected String doInBackground(String... f_url) {
          int count;
          try {
              URL url = new URL(f_url[0]);
              URLConnection conection = url.openConnection();
              conection.connect();
              // Get Music file length
              int lenghtOfFile = conection.getContentLength();
              // input stream to read file - with 8k buffer
              InputStream input = new BufferedInputStream(url.openStream(),10*1024);
              // Output stream to write file in SD card
              OutputStream output = new FileOutputStream(f_url[1]);
              byte data[] = new byte[1024];
              long total = 0;
              while ((count = input.read(data)) != -1) {
                  total += count;
                  // Publish the progress which triggers onProgressUpdate method
                  publishProgress("" + (int) ((total * 100) / lenghtOfFile));
  
                  // Write data to file
                  output.write(data, 0, count);
              }
              // Flush output
              output.flush();
              // Close streams
              output.close();
              input.close();
          } catch (Exception e) {
              Log.e("Error: ", e.getMessage());
          }
          return null;
      }
  
      // While Downloading Music File
      protected void onProgressUpdate(String... progress) {
          // Set progress percentage
          prgDialog.setProgress(Integer.parseInt(progress[0]));
      }
  
      // Once Music File is downloaded
      @Override
      protected void onPostExecute(String file_url) {
          // Dismiss the dialog after the Music file was downloaded
          dismissDialog(progress_bar_type);
          Toast.makeText(getApplicationContext(), "Download complete, playing Music", Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>Please how can i make the progress move according as the downloading time is moving. Thanks for helping.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think this problem is not the same as the one referred to be duplicate, the problem with mine after research is coming from the <code>int lenghtOfFile = conection.getContentLength();</code>  </p>
  
  <p>The progress is not moving because the <code>getContentLength()</code> always returns -1 because its unable to get the file size from the server. I have seen a lot of questions like this which are not answered. Please is there a way out? I would be grateful to know. Thanks Advance</p>