首页 文章

文件下载百分比为 74%

提问于
浏览
0

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

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

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

相关问题