Jenson Jenson - 1 year ago 218
Android Question

Show progress value for volley file download

I need to show the progress of file download in percentage.

Currently I am using Volley library. I use

class to make the download request and
to read/write the file.

How can I show the progress update in the most efficient manner?

Answer Source

As you have mentioned that you are using InputStreamVolleyRequest, I hope you have written the following code or something similar as well:

public void onResponse(byte[] response) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    try {
        if (response!=null) {

            String content =request.responseHeaders.get("Content-Disposition")
            StringTokenizer st = new StringTokenizer(content, "=");
            String[] arrTag = st.toArray();

            String filename = arrTag[1];
            filename = filename.replace(":", ".");
            Log.d("DEBUG::FILE NAME", filename);

                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 = != -1) {
                    total += count;
                    output.write(data, 0, count);


            }catch(IOException e){

    } catch (Exception e) {

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...");

Then just modify the while loop as shown below:

while ((count = != -1) {
    total += count;
    output.write(data, 0, count);
    progress = (int)total*100/file_length;

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download