Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:54:29+00:00 2026-06-18T05:54:29+00:00

I’ve read almost every similar topic but I couldn’t find a solution. So I

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:54:30+00:00Added an answer on June 18, 2026 at 5:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.