ProgressBar not updating with AsyncTask

I am using ProgressBar to try and display the progress of loading and saving a file. The ProgressBar appears, but stays at 0 until it closes when the task is complete. I've tried different approaches but it just doesn't update. Is there something wrong with the code?

class downloadData extends AsyncTask<Void, Integer, Void>
{

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        int count; 
        try 
        {

            URL url = new URL("http://google.com");
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            InputStream is = url.openStream();
            File testDirectory = new File(MainActivity.this.getFilesDir(), "downloadedData.txt");
            if (!testDirectory.exists()) 
            {
                testDirectory.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(testDirectory+"/downloadedData.txt");
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = is.read(data)) != -1) 
            {
                total += count;

                int neki = (int)(((double)total/lenghtOfFile)*100);
                this.publishProgress(neki);

                fos.write(data, 0, count);
            }
            is.close();
            fos.close();
        } 
        catch (Exception e) 
        {
            Log.e("ERROR DOWNLOADING","Unable to download" + e.getMessage());
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        super.publishProgress(values);
        progressDialog.setProgress(values[0]);

    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        progressDialog.dismiss();
    }
}

      

OnCreate

    progressDialog = new ProgressDialog(this);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setCancelable(true);
    progressDialog.setMax(100);
    progressDialog.setMessage("Downloading Data");

      

And when the button is clicked that starts the download: progressDialog.show();

+3


source to share


4 answers


Try to debug where progress is calculated. It might be a throwing issue, you can try to do it in 2 steps to avoid getting 0 after your split. But again, you have to create a variable with this progress value, put a breakpoint there and see the casting problem!



0


source


I'm not sure if the problem might be in your code, but you have this progressDialog.incrementProgressBy(incrementValue);



0


source


This may sound a little overwhelming, but have you tried the invalidate()

ProgressBar after you've set progress? I'm not sure if it does this automatically when you set progress.

0


source


I also faced the same problem and spent hours on it. The problem is in calculating the progress value.

 int neki = (int)(((double)total/lenghtOfFile)*100);

      

Instead, you must perform the calculation in two steps:

int neki = total * 100;
neki = (int)(neki/lengthOfFile);

publishProgress(neki);

      

This solved my problem. Hope this helps.

0


source







All Articles