我需要以百分比显示文件下载的进度。显示排除文件下载的进度值
目前我正在使用Volley库。我使用InputStreamVolleyRequest
类来创建下载请求,并使用BufferedOutputStream
来读取/写入文件。
如何以最有效的方式显示进度更新?
我需要以百分比显示文件下载的进度。显示排除文件下载的进度值
目前我正在使用Volley库。我使用InputStreamVolleyRequest
类来创建下载请求,并使用BufferedOutputStream
来读取/写入文件。
如何以最有效的方式显示进度更新?
正如你刚才提到你正在使用InputStreamVolleyRequest
,我希望你写了下面的代码或类似的东西还有:
@Override
public void onResponse(byte[] response) {
HashMap<String, Object> map = new HashMap<String, Object>();
try {
if (response!=null) {
String content =request.responseHeaders.get("Content-Disposition")
.toString();
StringTokenizer st = new StringTokenizer(content, "=");
String[] arrTag = st.toArray();
String filename = arrTag[1];
filename = filename.replace(":", ".");
Log.d("DEBUG::FILE NAME", filename);
try{
long lenghtOfFile = response.length;
InputStream input = new ByteArrayInputStream(response);
File path = Environment.getExternalStorageDirectory();
File file = new File(path, filename);
map.put("resume_path", file.toString());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果你已经做到了这一点,把一个进度条很容易。 得到ProgressDialog
对象并初始化,如下图所示:
progressDialog = new ProgressDialog(Activity Context here);
progressDialog.setTitle("Any Title here");
progressDialog.setMessage("Downloading in Progress...");
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
然后,只需修改while循环,如下图所示:
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
progress = (int)total*100/file_length;
progressDialog.setProgress(progress);
}
试试这个,让我知道。
但是让我告诉你,Volley并不适合大量下载。相反,我建议你使用DownloadManager
或Apache的HttpClient
甚至AsyncTask
。它们更易于使用,可能更适合此目的。
我使用进度条与HttpClient的,所以如果你想要做的进度,然后我的建议是使用HTTP客户端,这样就可以使正是你想要
希望这将帮你 !干杯!
这不回答原来的问题。 –
谢谢。你的解决方案帮了我。 – Newbie
我们欢迎..快乐编码! –
isnt'onResponse'文件完全下载后调用? –