0

I am trying to create a download progress bar while i am downloading files. I followed this tutorial however i can only make the progress bar count when i download files like images or mp3s.

What i need to be able to download are api responses such as these, but i cannot get their file size in order to have a reference for my progress bar.

        URL url = new URL(f_url[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int lenghtOfFile = connection.getContentLength();

When used on the API responses, the file size are -1 so the whole function is wrong.

What is a way to identify their sizes or any alternative way to create a progress bar while you download these.

Edit: I already am using Async task and it is working, the only problem is that i cannot increment my progress bar since i cannot get a file size.

linus
  • 493
  • 5
  • 17
  • you can use Async task..... – Piyush Aug 22 '13 at 12:28
  • @PiyushGupta: sorry forgot to mention, i am using async task already. I just need to know how to identify the file size of the file i am downloading or a way to increment the count for my progress dialog. – linus Aug 22 '13 at 12:29
  • you can refer this link..Useful to you.. http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Piyush Aug 22 '13 at 12:33

1 Answers1

3

And you can also use this:

class DownloadFileFromURL extends AsyncTask<String, String, String> {

/**
 * Before starting background thread
 * Show Progress Bar Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(progress_bar_type);
}

/**
 * Downloading file in background thread
 * */
@Override
protected String doInBackground(String... f_url) {
    int count;
    try {
        URL url = new URL(f_url[0]);
        URLConnection conection = url.openConnection();
        conection.connect();
        // getting file length
        int lenghtOfFile = conection.getContentLength();

        // input stream to read file - with 8k buffer
        InputStream input = new BufferedInputStream(url.openStream(), 8192);

        // Output stream to write file
        OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            // After this onProgressUpdate will be called
            publishProgress(""+(int)((total*100)/lenghtOfFile));

            // writing data to file
            output.write(data, 0, count);
        }

        // flushing output
        output.flush();

        // closing streams
        output.close();
        input.close();

    } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
    }

    return null;
}

/**
 * Updating progress bar
 * */
protected void onProgressUpdate(String... progress) {
    // setting progress percentage
    pDialog.setProgress(Integer.parseInt(progress[0]));
 }

/**
 * After completing background task
 * Dismiss the progress dialog
 * **/
@Override
protected void onPostExecute(String file_url) {
    // dismiss the dialog after the file was downloaded
    dismissDialog(progress_bar_type);

    // Displaying downloaded image into image view
    // Reading image path from sdcard
    String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
    // setting downloaded into image view
    my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}

 }
Piyush
  • 24,288
  • 6
  • 40
  • 72