【问题标题】:length shows minus in progress bar长度在进度条中显示减号
【发布时间】:2019-11-18 14:20:10
【问题描述】:

我正在从 URL 下载视频,当我试图获取进度时它显示负值,在下面我添加了我的代码,

byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = is.read(buffer)) != -1) {
    if (cancelDialogStatus) {
        break;
    }
    Log.e("System out", "doInBackground: progress:::" + len1);
    total += len1;
    // publishing the progress....
    // After this onProgressUpdate will be called
    int prg = (int) ((total * 100) / lenghtOfFile);
    Log.d("System out", "doInBackground: progress:::" + prg);
    if (prg > 100) {
        publishProgress(""
                + 100);
        fos.write(buffer, 0, len1);
        break;
    } else {
        publishProgress(""
                + (int) ((total * 100) / lenghtOfFile));
        fos.write(buffer, 0, len1);
    }
}

【问题讨论】:

  • 你是如何获得 lenghtOfFile 的?
  • HttpURLConnection con = null;试试 { downloadTimeStr = ""; URL url = 新 URL("url") url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(Constants.STANDARD_TIMEOUT); // con.setDoOutput(true);试试 { con.connect(); } catch (Exception ex) { //ex.printStackTrace(); } long lenghtOfFile = con.getContentLength();
  • 你有没有经过this solution这基本上是由于服务器没有设置header参数。就像您从 gdrive 下载时一样,在下载过程中它不会显示总文件大小。
  • 好的,非常感谢...您解决了我的问题
  • 你的文件长度是-1

标签: android android-asynctask android-progressbar


【解决方案1】:

这基本上是由于服务器没有设置Content-Length 标头参数。就像您从 Gdrive 下载时一样,在下载过程中它不会显示总文件大小。

简单的答案是内容长度未知。或者更具体地说,服务器没有在响应消息中设置 Content-Length 标头。

详细解决办法described here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 2013-02-22
    • 1970-01-01
    • 2014-05-25
    • 2023-03-10
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多