I use okhttp library for download files in android. I download successfully. But something is wrong when I pause and resume download.
Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();
File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
input.skip(downloadedSize);
} else {
output = new FileOutputStream(file, false);
}
long totalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
downloadedSize += count;
output.write(data, 0, count);
}
The problem is that for example size of file is 10MB. I pause when it downloaded 3MB and then resume to download and when download is finish size of file become 13MB. It doesnt start from downloaded size on resume, it start download from begining of bytestream. so file become 13MB. What is wrong with code?
Your file download will appear at the top of your notifications. Tap the Pause button. This button is located below the name of the file you're downloading. It will pause your download until you decide to resume.
Chosen solution You should be able to right click on the download and click Pause you can then click Resume later.
Just pause the download before shutting down your computer. When you turn it back on you only have to resume it in the download section (in settings or press CRTL-J). It takes a while to restart and might not work for every download but it worked for me, hope it helps future users!
You can pause by set the Downloads. Impl. COLUMN_CONTROL to Downloads.
FIRST WAY
I tried a lot of codes and finally I solved with BufferedSource source = responseBody.source(); source.skip(downloadedSize);
Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();
BufferedSource source = responseBody.source();
if(isResume)
source.skip(downloadedSize);
File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
} else {
output = new FileOutputStream(file, false);
}
long currentDownloadedSize = 0;
long currentTotalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
currentDownloadedSize += count;
output.write(data, 0, count);
}
It worked successfully. I think I'm lucky :)
SECOND WAY
I added header for skip downloaded bytes and it worked.
Request.Builder requestBuilder = new Request.Builder();
if (isResume) {
requestBuilder.addHeader("Range", "bytes=" + String.valueOf(downloadedSize) + "-");
}
Response request = requestBuilder.url(url).build();
ResponseBody responseBody = response.body();
BufferedSource source = responseBody.source();
File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
} else {
output = new FileOutputStream(file, false);
}
long currentDownloadedSize = 0;
long currentTotalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
currentDownloadedSize += count;
output.write(data, 0, count);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With