开发者

File download progress bar in android

开发者 https://www.devze.com 2023-03-05 02:02 出处:网络
How to show 开发者_如何学Cthe remaining KB\'s of file is to be downloaded in progress bar in android.

How to show 开发者_如何学Cthe remaining KB's of file is to be downloaded in progress bar in android. e.g 12kb/120kb is remaining..then 97kb/120kb...etc

Can we have this progress dialog as shown in the image

File download progress bar in android


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);
        }
    }


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

File download progress bar in android


progressDialog.setMax(fileLength/1000);

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

The below code is only available after API 11

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


Take a look at http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog


You can use a progress dialog, as discussed in the docs. Alternatively (and perhaps more elegantly), you can use a progress indicator in your activity's title bar. In your activity, before calling setContentView, add this line:

requestWindowFeature(Window.FEATURE_PROGRESS);

Then by calling setProgress(int) you can indicate progress. When progress reaches 10000, the progress indicator in the title bar fades away. Note that if you determine progress in a non-UI thread, you should use a Handler to call setProgress.

0

精彩评论

暂无评论...
验证码 换一张
取 消