0

I have read this topic

and I have a problem about downloading large files, because progress dialog does not upload progress or upload only once after downloading and show 100% suddenly. But all works perfectly when I'm downloading small files.

I think problem may be here:

publishProgress((int)(total*100/fileLength));

My code:

private class DownloadFile extends AsyncTask<String, Integer, String> {

    @Override

    protected String doInBackground(String... sUrl) {

        try {

            URL url = new URL(sUrl[0]);

            URLConnection connection = url.openConnection();

            connection.connect();

            // this will be useful so that you can show a typical 0-100% progress bar

            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());

            String sdpath = Environment.getExternalStorageDirectory().getAbsolutePat();

            File dir = new File(sdpath + "/Myfolder");

            dir.mkdir();

            OutputStream output = new FileOutputStream(dir.toString() + "/" + "myfilename");

            byte data[] = new byte[1024];

            long total = 0;

            int count;

            while ((count = input.read(data)) != -1) {

                total += count;

                // publishing the progress....

               publishProgress((int)(total/fileLength)*100);

               output.write(data, 0, count);

            }
            output.flush();
            output.close();
            input.close();
            } 
          catch (Exception e) {

        }

        return null;

    }

    @Override

    protected void onPreExecute() {

        super.onPreExecute();

        mProgressDialog.show();

    }

      @Override

       protected void onProgressUpdate(Integer... progress) {

        super.onProgressUpdate(progress);

        mProgressDialog.setProgress(progress[0]);

    }

}

My file is about 80 mb.

Community
  • 1
  • 1
Derek K
  • 2,756
  • 1
  • 21
  • 37

1 Answers1

0

You should try to divide first :

publishProgress((int)(total/fileLength)*100);

as you can get an out of range division.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • It didn't help me, still not working. PS I noticed that progress is updating to 100% when file is about 30 mb (but file is still downloading to the end) – Derek K Dec 15 '12 at 11:54
  • Please edit your question, lots of things to say. And notify me when done, please. – Snicolas Dec 15 '12 at 12:22