我正在从服务器下载文件,但有一个小问题:下载完成后,文件的百分比始终停止在 74%,但下载完成了 100%。
FileOutputStream output = new FileOutputStream(mFile);
int fileSize = mConnection.getContentLength();
InputStream input = mConnection.getInputStream();
byte[] buffer = new byte[1024]; // I think its this line that would be causing the issue
int count;
int total = 0;
while ((count = input.read(buffer)) != -1) {
total += count;
if (fileSize > 0)
publishProgress((int) (total * 100 / fileSize));
output.write(buffer, 0, count);
if (isCancelled())
break;
}
我认为是byte[] buffer = new byte[1024]
引起问题的行,因为它可能无法正确计算文件大小或沿这些行行吗?
我希望它以 100%而不是 74%完成
1 回答
由于文件下载在
74%
之后结束,因此文件不会发布更改,因为文件下载完成后不会再次调用 background 方法。您可以在onPostExecute
方法上发布更改