【发布时间】:2017-02-01 21:01:12
【问题描述】:
我正在尝试使用 Asynctask 从 URL 下载文件。在 doInBackground() 方法中,我检索文件大小,如下所示。
lenghtOfFile = conection.getContentLength();
这将返回一个整数值。现在,下载继续进行,文件已完全下载并写入 sdcard。
问题是在我的 postExecute() 中我想比较来自 URL 的文件的大小和下载的文件的大小。
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//check if the file was download and know how much of it
//was downloaded, so as to update ui accordingly
if(file.exists()){
Log.e("apiFileSize", String.valueOf(lenghtOfFile));
Log.e("downloadFileSize", String.valueOf(file.length()));
//check if the file was completely downloaded
if(lenghtOfFile == file.length()){
....
如您所见,我记录了这两个值,但即使下载运行到最后并且文件已完全下载,它们也不相等。本地存储(sdcard)中的文件最终大于 url 中的文件大小。我希望他们是平等的,为什么会这样?我该怎么办?
以下是日志结果。
09-23 22:44:48.203 26594-26594/com.mobile.soullounge E/apiFileSize﹕ 1298573
09-23 22:44:48.204 26594-26594/com.mobile.soullounge E/downloadedFileSize﹕ 2696392
提前致谢。
【问题讨论】:
-
下面的回答对你有用吗?如果愿意接受?
标签: android url filesize download