0

I am trying to download a 600 MB file using an AsyncTask. If I execute my code on a real-world device, it works, but only if I "keep an eye on it" by not allowing the screen to remain off for any length of time, however, if I do not do this and just leave the device on its own, when I go to check on it a few minutes later, the progress dialog is gone and the file it has downloaded is only a fraction of the size it should be. My guess it that it has something to do with the phone putting WiFi to sleep after the screen's being off for a certain amount of time.

How do I fix this?

Here is the relevant code:

class DownloadFileAsync extends AsyncTask<String, String, String> {

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

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

        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();


            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/file.zip");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);
            }

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

    }
    protected void onProgressUpdate(String... progress) {

         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
}
} 
Frank Bozzo
  • 15,033
  • 6
  • 25
  • 29
  • 2
    try to stick with existing implementation. The downloadManager exists to do stuff like this for you, effortlessly: http://developer.android.com/reference/android/app/DownloadManager.html – Ian Aug 09 '11 at 14:52
  • API Level is too high, need more backward compatibility... thanks anyway though! – Frank Bozzo Aug 09 '11 at 14:59

3 Answers3

2

This question describes how to keep the screen on so the phone does not go to sleep. If you are trying to download files 600mb+, you might consider putting your application into some type of Service as this would potentially take a few hours.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
1

Also you could use Download Manager if your target SDK is high enough.

renam.antunes
  • 761
  • 1
  • 5
  • 11
0

Most likely you are using Wi-Fi connection, which can be disconnected as soon as screen is off. You can check this in:

  1. Settings-> Wireless and Networks -> Wi-Fi Setting
  2. Click , select Advanced
  3. Click on "Wi-Fi sleep policy" and select "never".

But you should consider using Download Manager for newer devices (with Android 2.3+).

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
inazaruk
  • 74,247
  • 24
  • 188
  • 156