图像部分下载,但响应和写入接收器成功

时间:2018-07-04 04:22:54

标签: android http picasso okhttp okhttp3

我使用okhttp(android)在我的应用程序中下载图像。但是有时映像下载会以部分映像完成,但是okhttp返回成功的响应,并使用okio /缓冲接收器成功写入接收器。下图说明了我的意思 enter image description here

我使用以下代码下载图像并将其保存到设备:

Request request = new Request.Builder().url(url).tag(fileName).build();
final Call downloadCall = new OkHttpClient().newCall(request);
downloadCall.enqueue(new Callback() {
public void onResponse(Call call, Response response) {
        if (downloadCall.isCanceled() || call.isCanceled())
            return;

        if (!response.isSuccessful()) {
            requestCallback.onFailure();
            return;
        }
        try {
            if (response.body() != null) {
                BufferedSink sink = Okio.buffer(Okio.sink(tempFile));
                sink.writeAll(response.body().source());
                sink.close();
                response.body().close();
                if (FileUtils.moveFile(tempFile, downloadedFile))
                    requestCallback.onSuccess();
                else
                    requestCallback.onFailure();
            } else {
                FileUtils.deleteFile(tempFile);
                requestCallback.onFailure();
            }
        } catch (Exception e) {
            FileUtils.deleteFile(tempFile);
            requestCallback.onFailure();
        }
    }

    public void onFailure(Call call, IOException e) {
        if (downloadCall.isCanceled() || call.isCanceled())
            return;
        FileUtils.deleteFile(downloadedFile);
        requestCallback.onFailure();
    }

});

(PS:当我检查本地存储中的文件时,与上面相同,这不是渲染问题) 难道我做错了什么?还是HTTP客户端问题?

0 个答案:

没有答案