簡體   English   中英

如何顯示下載進度?

[英]How to Show Downloading Progress?

媒體下載時,我試圖顯示進度。 歌曲已正確下載和存儲,現在我要顯示百分比

誰能幫我。 這是我的代碼:

class DownloadFile extends AsyncTask<String, String, String> {
                    @Override
                    protected String doInBackground(String... url) {

               //............ declration and all
       if (fsize != lenghtOfFile) {
                        int filesize = f.getAbsolutePath().length();
                        if (filesize != lenghtOfFile) {
                            InputStream input = new BufferedInputStream(url1.openStream());
                            OutputStream output = new FileOutputStream(f);
                            byte data[] = new byte[1024];
                            long total = 0;
                            while ((count = input.read(data)) != -1) {
                                total += count;
                                publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                              //  pDialog.setMessage( String.valueOf((int) ((total * 100) / lenghtOfFile)) +"Downloaded.");
                                output.write(data, 0, count);
                            }
                            output.flush();
                            output.close();
                            input.close();
                        }
                 //...... other code
                    }

                    @Override
                    protected void onProgressUpdate(String... values) {
                        super.onProgressUpdate(values);
                    }

                    @Override
                    protected void onPostExecute(String result) {
                    //on post over  
                      pDialog.dismiss();
                    }
                    @Override
                    protected void onPreExecute() {
                   //set Dialog               
                  pDialog = new ProgressDialog(getActivity());
                        pDialog.setMessage("Please wait song is Downloading...");
                        pDialog.setIndeterminate(false);
                        pDialog.setCancelable(false);
                        pDialog.show();
                    }
                }

**一切正常。...我只想顯示剩余百分比,例如99%,98%仍然...明智

只需更換

publishProgress("" + (int) ((total * 100) / lenghtOfFile));

publishProgress("" + (int) (((lenghtOfFile - total) * 100) / lenghtOfFile));

前者從0增長到100,但后者從100增長到0。

只需添加以下代碼:

     protected void onPreExecute() {
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Downloading...");
                pDialog.setIndeterminate(false);
                pDialog.setMax(100);
                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pDialog.setCancelable(true);
                pDialog.show();
            }
 @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            pDialog.setProgress(Integer.parseInt(values[0]));
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM