【发布时间】:2020-08-11 21:15:18
【问题描述】:
我正在下载视频文件,它工作正常并在成功下载后播放,但如果我暂停然后继续下载,那么它会成功下载剩余的文件,但它已损坏且无法播放视频
Request.Builder builder = new Request.Builder();
builder = builder.url(downloadFileUrl);
builder = builder.addHeader("RANGE", "bytes=" + existLocalFileLength);
Request request = builder.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
if (response != null && response.isSuccessful()) {
RandomAccessFile downloadFile = new RandomAccessFile(existLocalFile, "rw");
downloadFile.seek(existLocalFileLength);
ResponseBody responseBody = response.body();
InputStream inputStream = responseBody.byteStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte data[] = new byte[102400];
long totalReadLength = 0;
int readLength = bufferedInputStream.read(data);
while (readLength != -1) {
if (getDownloadManager().isDownloadPaused()) {
ret = DOWNLOAD_PAUSED;
break;
} else if (getDownloadManager().isDownloadCanceled()) {
ret = DOWNLOAD_CANCELED;
break;
} else {
downloadFile.write(data, 0, readLength);
totalReadLength = totalReadLength + readLength;
int downloadProgress = (int) ((totalReadLength + existLocalFileLength) * 100 / downloadFileLength);
Log.d("download_progress", "download_progress..." + downloadProgress);
}
readLength = bufferedInputStream.read(data);
}
}
【问题讨论】:
标签: java android file kotlin download