Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resume Download not working in android

This code for resuming download is not working properly in Android, although it works fine in a Java application. Here I am trying to download a zip file, and it will resume the download, but the net result is an invalid zip file.

 BufferedInputStream in = null;
        FileOutputStream fos = null;
        BufferedOutputStream bout=null;

        try {
            downloaded=0;
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
                File file=new File(DESTINATION_PATH);
                if(file.exists()){
                    downloaded = (int) file.length();
                }
            }
            connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
            connection.connect();
            size=connection.getContentLength();
            Dialog.setMax(size);
             in = new BufferedInputStream(connection.getInputStream());
             fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
             bout = new BufferedOutputStream(fos, 1024);
            byte[] data = new byte[1024];
            int x = 0;
            while ((x = in.read(data, 0, 1024)) >= 0) {
                bout.write(data, 0, x);
                 downloaded += x;
                 System.out.println(downloaded);
                 onProgressUpdate((int)(downloaded*100/size));
            }

            succes=true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

Thanks.

like image 927
Lijo Joy Avatar asked Jan 04 '11 11:01

Lijo Joy


People also ask

How can I resume download in Android?

Press Ctrl + J or click the Options dropdown menu and select Downloads to open the download manager. In the list of downloads, find the failed item and click Resume. If everything goes to plan, your download will resume from where it got interrupted.


1 Answers

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int buf = 1024;

if (ISSUE_DOWNLOAD_STATUS.intValue() == ECMConstant.ECM_DOWNLOADING) {
    File file = new File(DESTINATION_PATH);
    if (file.exists()) {
         downloaded = (int) file.length();
         connection.setRequestProperty("Range",
             "bytes=" + file.length() + "-");
    }
} else {
    connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}

connection.setDoInput(true);
connection.setDoOutput(true);

progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos = new FileOutputStream(DESTINATION_PATH, downloaded == 0 ? false : true);
bout = new BufferedOutputStream(fos, buf);
byte[] data = new byte[buf];

while ((int x = in.read(data, 0, buf)) >= 0) {
    bout.write(data, 0, x);
    downloaded += x;
    progressBar.setProgress(downloaded);
}
like image 86
Lijo Joy Avatar answered Oct 05 '22 04:10

Lijo Joy