How to pause/resume downloading with okhttp in Android
Asked Answered
M

4

14

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?

Mcrae answered 11/4, 2017 at 17:27 Comment(3)
You're creating a new request and sending it. That causes the response to hold the entire file. I'm not sure if okHttp has the ability to download partial responses, but if it does that's not how you do it. You'd at the least have to pass in some data about where to start the download from.Ribbon
I don't see any problem with your approach may be your download size was 0 which in turns skip nothing. I really don't find any difference between the question and the answer described in a first way. If there is any specific difference then do let me knowBerryman
@Killer differences is source.skip(downloadedSize) . The code that you dont see I get file download status and downloaded size from db. For example file size is 5.5mb and I downloaded 3mb in past download process. The next downloading process must call source.skip(3mb to bytes)Mcrae
M
24

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);   
}
Mcrae answered 11/4, 2017 at 21:37 Comment(5)
How to pause the download in the first place? Can you share me some ideas on how to handle the resume part on the button click?Circinate
@dazed'n'confused sorry but I could not understand your question. What is your problem with resume/pause ?Mcrae
I am trying to download files using a service and I need to pause the download in the service how can i do that?Circinate
@dazed'n'confused you need to save last downloaded size of file when it paused. On resume you will use downloaded size for starting download from which bytes. Actually your question's answer is in my answer https://mcmap.net/q/805278/-how-to-pause-resume-downloading-with-okhttp-in-androidMcrae
second way is better, in first way when you use skip, client must download file then skip N byte from first. for example you start download 100 MB file and resume it at 50 MB, when you start again client download from first but skip 50 MB and after 50 MB you can read output.Speckle
E
1
val call = client.newCall(request)

call.enqueue(object : Callback {
    override fun onFailure(call: Call, e: IOException) {
        listener.onFail(e)
    }

    override fun onResponse(call: Call, response: Response) {
        // write the stream to a file (downloading happening)
        val stream = response.body?.byteStream()
    }
})

// this will stop the downloading
call.cancel()

to resume use the "Range" header with your request and append the stream to already downloaded file.

Ernaernald answered 14/4, 2020 at 18:16 Comment(0)
C
0

Based on Alexander answer, I have tried both ways he recommended. The first way produced errors when download stopped and then continued after an app restart. I have found the second way is more stable. Also there is a syntax error in the code. A sample code is here.

        File file = new File(path);
        long availableFileLength = file.exists() && file.isFile() ? file.length() :0;
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.addHeader("Range", "bytes=" + String.valueOf(availableFileLength) + "-");
        Request request = requestBuilder.url(url).build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)
            .build();
        ResponseBody responseBody = okHttpClient.newCall(request).execute().body();

        InputStream input = new BufferedInputStream(responseBody.byteStream());
        OutputStream output = new FileOutputStream(downloadPath, true);

        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);   
        }
Clarabelle answered 19/6, 2023 at 13:1 Comment(0)
M
0

How I did it with OkHttp + Okio

Save the totalBytes downloaded and add tag to the request so you can cancel it to pause download.

        Request.Builder requestBuilder = new Request.Builder();
        if(isResume) requestBuilder.addHeader("Range","bytes="+downloadedSize+"-");
        okHttpClient.newCall(requestBuilder.url(downloadUrl).tag(filePath).build()).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {e.printStackTrace();}
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                if(response.isSuccessful()){
                    BufferedSink sink=null; BufferedSource source=null;
                    try {
                        ResponseBody responseBody = response.body();assert responseBody != null;
                        source = responseBody.source(); 
                        File downloadFile = new File(filePath);
                        sink = Okio.buffer(downloadFile.exists() ? Okio.appendingSink(downloadFile) : Okio.sink(downloadFile)); //append if resume
                        int bufferSize = 8 * 1024;
                        for (long bytesRead; (bytesRead = source.read(sink.getBuffer(), bufferSize)) != -1; ) {
                            sink.emit(); downloadedSize+=bytesRead;
                            publishProgress(downloadedSize);// you can show download progress update. You can use Handler to update UI on main thread 
                        }
                        Log.d(TAG, "File successfully downloaded and saved to storage.");
                    }
                    catch (IOException e){e.printStackTrace();}
                    finally {
                        try {if(sink!=null) {sink.flush();sink.close();} if(source != null)source.close();}catch (IOException e){e.printStackTrace();}
                    }
                }
            }
        });

To pause download, cancel the request using the tag;

    private void cancel(String tag){
        for(Call call: okHttpClient.dispatcher().queuedCalls()){
            if(tag.equals(call.request().tag())) call.cancel();
        }
        for(Call call: okHttpClient.dispatcher().runningCalls()){
            if(tag.equals(call.request().tag())) call.cancel();
        }
    }
Mischance answered 21/5 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.