DKV
DKV

Reputation: 1767

progress bar does't response when downloading

i am downloading a file from a server. i wand to show the progress bar while i am downloading. when i am trying to down load the file its shows the progress bar. its shows 0 in the bar but i am able to down load the file. the code i use is given below

//onPreExecute

             protected void onPreExecute() {

                 super.onPreExecute();
                    showDialog(progress_bar_type);
                }

//this is an ask task fordownloading

protected String doInBackground(String... arg0) {
                      int bufferLength = 0;
                    try {
                                             //geting connection
                        URL url = new URL(  weburl);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setRequestMethod("GET");
                        urlConnection.setDoOutput(true);
                        urlConnection.connect();
                     // getting file length
                        int lenghtOfFile = urlConnection.getContentLength();

                        File sdcard = Environment.getExternalStorageDirectory();
                        File file = new File(sdcard, "filename.extension");

                        FileOutputStream fileOutput = new FileOutputStream(file);
                        InputStream inputStream = urlConnection.getInputStream();

                        byte[] buffer = new byte[1024];

                        long total = 0; 
                         while ( (bufferLength = inputStream.read(buffer))!= -1) {
                            total += bufferLength ;
                                              //progress
                            publishProgress(""+(int)((total*100)/lenghtOfFile));
                            fileOutput.write(buffer, 0, bufferLength);
                        }

i use an onProgressUpdate to show the result but no use . it is still in 0 only

 protected void onProgressUpdate(String...values) {
                        // setting progress percentage
                        pDialog.setProgress(Integer.parseInt(values[0]));
                   }

//onPostExecute

                    protected void onPostExecute(final Boolean success) {

                        dismissDialog(progress_bar_type);}

Upvotes: 1

Views: 197

Answers (3)

Basbous
Basbous

Reputation: 3925

long total = 0;
int iterationCount = 0;
int progress = 0;
while ( (bufferLength = inputStream.read(buffer))!= -1) {
total += bufferLength ;
progress = (int)((total*100)/lenghtOfFile)
if(iterationCount >= 14 || progress == 100) {
     iterationCount = 0;
     publishProgress(String.valueOf(progress));
} else {
     iterationCount += 1;
}

fileOutput.write(buffer, 0, bufferLength);
}

According to your problem of getting the length of the file equal to -1

This information will not always be available. Usually you will know the length of the file you are downloading. Depending on the webserver, the protocol, the connection, and the method of downloading, this information may not always be available.

You should definitely modify your application so that it can handle this situation. I think you will find that different devices using different connection methods will offer different results with this.

Upvotes: 3

Piyush
Piyush

Reputation: 2050

You need to add one more override method in asynctask

@Override
protected void onProgressUpdate(String... values) {
  // TODO Auto-generated method stub
  super.onProgressUpdate(values);
  inDeterminatePB.setProgress(Integer.parseInt((values[0])));
}

Upvotes: 1

Swetank
Swetank

Reputation: 1597

Use onProgressUpdate() method of AsyncTask to update your progress bar.

Refer link

Upvotes: 1

Related Questions