【发布时间】: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