文件下载百分比为74%

时间:2018-12-12 09:07:59

标签: java android

我正在从服务器下载文件,但有一个小问题:下载完成后,文件的百分比总是停止在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 个答案:

答案 0 :(得分:0)

由于文件下载在74%之后结束,因此文件不会发布更改,因为文件下载完成后不会再次调用background方法。 您可以在onPostExecute方法上发布更改

void onPostExecute(String result){
   publishProgress(100);
}