1

This is my doInBackground method:

@Override
    protected String doInBackground(String... sUrl) {

        InputStream input = null;

        HttpURLConnection conection = null;

        BufferedOutputStream bout = null;
        FileOutputStream fos = null;


        int downloaded = 0;


        try {
            URL url = new URL(sUrl[0]);
            conection = (HttpURLConnection)url.openConnection();
            int lenghtOfFile = conection.getContentLength();

            if(STATUS) {
                File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk");
                if (file.exists()) {
                    downloaded = (int) file.length();
                    conection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");
                }
            }
            else {
                conection.setRequestProperty("Range", "bytes=" + downloaded + "-");
            }
            conection.setDoInput(true);
            conection.setDoOutput(true);

            conection.connect();

            input = new BufferedInputStream(url.openStream(), 8192);
            fos=(downloaded==0)? new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk"): new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk",true);
            bout = new BufferedOutputStream(fos, 1024);
            byte data[] = new byte[1024];

            long total = 0;
            int count = 0;

            while ((count = input.read(data, 0, 1024)) >= 0) {
                if (isCancelled()) {
                    input.close();
                    return null;
                }

                bout.write(data, 0, count);
                downloaded += count;
                publishProgress((int)(downloaded * 100/ lenghtOfFile) );
                total += count;
            }
            bout.flush();
            input.close();
            fos.close();


        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
                if (fos != null)
                    fos.close();
                if (bout != null)
                    bout.close();
            } catch (IOException ignored) {
            }

            if (conection != null)
                conection = null;
        }

        return null;
    }

I start download task with this code (resume flag is false -> STATUS = FALSE):

 dt = new DownloadTask(DownloadsActivity.this, false);
 dt.execute("myurl.something.apk"); 

then when downloaded completely I launch apk file and all thing work correctly and apk installed correctly.

But when pause my download with this code:

dt.cancel(true);

and then resume it with this code (resume flag is true-> STATUS = TRUE):

 dt = new DownloadTask(DownloadsActivity.this, true);
 dt.execute("myurl.something.apk"); 

This time apk size is equal to last downloaded before pause + apk total size, therefore my apk file is corrupted.
Which means connection.setRequestProperty() not working for me.

What is my code problem?
Thanks in advance.

hoomi
  • 1,882
  • 2
  • 16
  • 17
Naruto Uzumaki
  • 2,019
  • 18
  • 37

0 Answers0