In my application I download media from server via HTTP connection. While downloading if connection is break anyhow (say, no n/w, permission revoke), File get downloaded partially.
Earlier we are deleting the file and downloading it from the scratch.
Now, the requirement is changed. I have to continue from where I left (like in WhatsApp).
So what I am doing is checking the file existence and if file exist skip the stream. Please find the below code snip.
InputStream is = con.getInputStream();
File file = new File(targetPath);
boolean fileExist = file.exists();
if (fileExist) {
long skippedBytes = is.skip(file.length());
}
// opens an output stream to save into file
OutputStream os = new FileOutputStream(targetPath, fileExist);
Its working merely in respect of skipping but actually not working.
Say, file (video) length is 20 sec and in first attempt I downloaded up to 6 sec and rest subsequently. But when I play it just play till 6 sec then after throw the usual Video playback error.
File file = new File(targetPath);
if (file.exists()) {
con.setRequestProperty("Range", "bytes=" + (file.length()) + "-");
} else {
con.setRequestProperty("Range", "bytes=" + 0 + "-");
}