显示排序文件下载的进度值 [英] Show progress value for volley file download
问题描述
我需要以百分比显示文件下载的进度。
目前我正在使用 Volley 库。我使用 InputStreamVolleyRequest
类来进行下载请求和 BufferedOutputStream
来读/写文件。
如何以最有效的方式显示进度更新?
提到你正在使用 InputStreamVolleyRequest
,我希望你写了以下代码或类似的东西:
@Override
public void onResponse(byte [] response){
HashMap< String,Object> map = new HashMap< String,Object>();
尝试{
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);
文件路径= Environment.getExternalStorageDirectory();
文件文件=新文件(路径,文件名);
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(异常e){
e.printStackTrace();
}
}
如果你已经这样做了,简单。
获取 ProgressDialog
对象和初始化如下所示:
progressDialog = new ProgressDialog(Activity Context here);
progressDialog.setTitle(Any Title here);
progressDialog.setMessage(正在下载...);
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
然后只需修改while循环,如下所示:
$ b $而((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
。他们更容易使用,也许更好地为此目的。
I need to show the progress of file download in percentage.
Currently I am using Volley library. I use InputStreamVolleyRequest
class to make the download request and BufferedOutputStream
to read/write the file.
How can I show the progress update in the most efficient manner?
As you have mentioned that you are using InputStreamVolleyRequest
, I hope you have written the following code or something similar as well:
@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();
}
}
If you have already done this, putting a progress bar is easy.
get ProgressDialog
object and initialise as shown below:
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();
Then just modify the while loop as shown below:
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
progress = (int)total*100/file_length;
progressDialog.setProgress(progress);
}
Try this and let me know.
However let me also inform you that Volley is not suitable for heavy download. Rather I suggest you to use DownloadManager
or Apache's HttpClient
or even AsyncTask
. They are easier to use and probably better for this purpose.
这篇关于显示排序文件下载的进度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!