Android file download progress bar

How to show the remaining KB of the file, you need to load in the progress bar in android. e.g. 12kb / 120kb and then 97kb / 120kb ... etc.

Can we have this run dialog as shown in the picture enter image description here

+3
source share
5 answers
class DownloadFileAsync extends AsyncTask<String, String, String> {
        private ProgressDialog mProgressDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(UrlTestActivity.this);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }

        @Override
        protected String doInBackground(String... aurl) {
            int count;
            try {

                for (int i = 0; i < 3; i++) {
                    URL url = new URL("http://nodeload.github.com/nexes/Android-File-Manager/zipball/master");
                    URLConnection conexion = url.openConnection();
                    conexion.connect();
                    int lenghtOfFile = conexion.getContentLength();
                    InputStream is = url.openStream();
                    File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder");
                    if (!testDirectory.exists()) {
                        testDirectory.mkdir();
                    }
                    FileOutputStream fos = new FileOutputStream(testDirectory+ "/"+(i+100)+".zip");
                    byte data[] = new byte[1024];
                    long total = 0;
                    int progress = 0;
                    while ((count = is.read(data)) != -1) {
                        total += count;
                        int progress_temp = (int) total * 100 / lenghtOfFile;
                        publishProgress(""+ progress_temp);
                        if (progress_temp % 10 == 0 && progress != progress_temp) {
                            progress = progress_temp;
                        }
                        fos.write(data, 0, count);
                    }
                    is.close();
                    fos.close();
                }
            } catch (Exception e) {}
            return null;

        }
        protected void onProgressUpdate(String... progress) {
             Log.d("ANDRO_ASYNC",progress[0]);
             mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
+2
source

The best way you described is to download using AsyncTask . In the onProgressUpdate method, you can update the ProgressDialog to indicate the percentage of completion to the user. It will look like this:

ProgressDialog Screenshot

+1
source
progressDialog.setMax(fileLength/1000);

publishProgress(String.valueOf(total /1000));

API 11

progressDialog. setProgressNumberFormat ("%1d kb of %2d kb");
+1

You can use the progress dialog as described in the docs. Alternatively (and perhaps more elegantly) you can use the progress indicator in the activity title bar. In your activity, before calling, setContentViewadd this line:

requestWindowFeature(Window.FEATURE_PROGRESS);

Then, by calling setProgress(int), you can indicate the progress. When progress reaches 10,000, the progress indicator in the title bar disappears. Please note: if you determine progress in a thread other than the UI, you must use a handler to call setProgress.

0
source

All Articles