ProgressBar is not updating while downloading a file in Android

I've read almost every similar topic but I couldn't find a solution. So I decided to ask my own.

I want to download a file and show a progress dialog during the operation. The file gets downloaded successfully.

The first problem is that the progress bar doesn't update during the download operation. It just stays %0 and when the download is finished, the dialog disappears as expected.

The second problem is - I noticed this by accident - when my activity gets updated (for example, when screen orientation is changed) the dialog disappears but the download continues.

I hope someone can help.

Here's my code:

package com.mehmetakiftutuncu.downloadunzipshow;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener
{
    final String LOG_TAG = "DownloadUnzipShow";

    final String RELATIVE_PATH = "mehmetakiftutuncu";
    final String FILE_NAME = "...";
    final String FILE_URL = "...";
    final String FULL_PATH = Environment.getExternalStorageDirectory().getPath() + "/" + RELATIVE_PATH + "/";;

    Button buttonDownload, buttonUnzip, buttonShow;
    ProgressDialog progressDialog;

    public class MyFileDownloader extends AsyncTask<String, String, String>
    {
        boolean isSuccessful = true;

        InputStream inputStream;
        OutputStream outputStream;

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

            Log.d(LOG_TAG, "Downloading: " + FILE_URL + " to " + FULL_PATH + FILE_NAME);
        }

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

            try
            {
                URL url = new URL(params[0]);
                URLConnection connection = url.openConnection();

                connection.connect();

                int lengthOfFile = connection.getContentLength();

                File path = new File(FULL_PATH);
                if(!path.exists())
                {
                    path.mkdir();
                }

                inputStream = new BufferedInputStream(url.openStream());
                outputStream = new FileOutputStream(FULL_PATH + FILE_NAME);

                byte[] data = new byte[1024];

                long total = 0;
                int percentage = 0;

                while((count = inputStream.read(data)) != -1)
                {
                    total += count;

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

                    publishProgress(String.valueOf(percentage));

                    outputStream.write(data, 0, count);
                }

                outputStream.flush();
                outputStream.close();
                inputStream.close();
            }
            catch(Exception e)
            {
                isSuccessful = false;
                Log.e(LOG_TAG, "An error occured while downloading. Details: " + e.getMessage());
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(String... values)
        {
            super.onProgressUpdate(values);

            progressDialog.setProgress(Integer.parseInt(values[0]));
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);

            if(isSuccessful)
            {
                Log.d(LOG_TAG, "File is successfully downloaded to: " + FULL_PATH + FILE_NAME);
            }

            progressDialog.dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonDownload = (Button) findViewById(R.id.button_download);

        buttonDownload.setOnClickListener(this);
    }

    private void prepareProgressDialog()
    {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(getString(R.string.dialog_title));
        progressDialog.setMessage(getString(R.string.dialog_message) + FILE_URL);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.button_download:
                new MyFileDownloader().execute(FILE_URL);
                break;
        }
    }
}

Upvotes: 0

Views: 1785

Answers (2)

user
user

Reputation: 87064

The first problem is that the progress bar doesn't update during the download operation. It just stays %0 and when the download is finished, the dialog disappears as expected.

That is happening because the line:

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

always return 0 as you're doing an integer java division where total is smaller than lengthOfFile. Make total a double value. Also don't cast the percentage as a String, modify the second generic argument of the AsyncTask to be an Integer.

The second problem is - I noticed this by accident - when my activity gets updated (for example, when screen orientation is changed) the dialog disappears but the download continues.

On a configuration change(a rotation for example) the Activity will be recreated killing your dialog in the process. If you want to re-show the dialog you'll need to keep the AsyncTask across the configuration change by passing it to the onRetainNonConfigurationInstance() method of the Activity(or you could use fragments). In the onCreate method use getLastNonConfigurationInstance to see if you have a task retained and still running and reshow the dialog. Also you really shouldn't make the AsyncTask a inner class in the Activity because you'll tie it to the activity.

Upvotes: 3

Gabe Sechan
Gabe Sechan

Reputation: 93559

THe dialog disappearing- are you capturing orientation config changes, or are you letting Android do it? If you're letting Android do it, it will kill your activity and recreate it, which will destroy your dialog. The async task wouldn't get killed because the async task is owned by the system. To fix that, add android:configChange="orientation" to your manifest for the activity. General rule of thumb is if you want to have dialogs persist through rotation you need to do that.

Upvotes: 0

Related Questions