0

for()ループの助けを借りてAsyncTaskで複数のファイルをダウンロードしています。 以下のコードは正常に動作しますが、各ファイルは独自のプログレスバーと1つのプログレスバーでダウンロードされます。ダウンロードしたすべてのファイルに対して1つの進捗バーが必要です。1つのプログレスバーで複数のファイルをダウンロードするjava/Android

// ProgressDialog for downloading images 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case progress_bar_type: 
      pDialog = new ProgressDialog(this); 
      pDialog.setMessage("Downloading file. Please wait..."); 
      pDialog.setTitle("In progress..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setMax(100); 
      pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
      return pDialog; 
     default: 
      return null; 
    } 
} 

下記ダウンロードファイルのためのAsyncTask ..です

class DownloadFileFromURL extends AsyncTask<String, Integer, 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 { 

      for (int i = 0; i < f_url.length; i++) { 
       URL url = new URL(f_url[i]); 
       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); 
       System.out.println("Data::" + f_url[i]); 
       // Output stream to write file 
       OutputStream output = new FileOutputStream(
         "/sdcard/Images/" + i + ".jpg"); 

       byte data[] = new byte[1024]; 

       long total = 0; 
       int zarab=20; 

       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(); 
       //cc++; 
      } 
     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return null; 
    } 

    /** 
    * Updating progress bar 
    * */ 
    protected void onProgressUpdate(Integer... progress) { 
     // setting progress percentage 
     pDialog.setProgress(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() + "/downloaded.jpg"; 
     // setting downloaded into image view 
     // my_image.setImageDrawable(Drawable.createFromPath(imagePath)); 
    } 

} 

場合やプログレスショーやアップグレードファイルの番号に関して代わりlenghtOfFileで、それはまた別の、有用ソリューションであることを意味します。 ご協力いただければ幸いです。

+0

こんにちは、私はあまりにも同じ問題で苦労しています。ありがとうございます。 –

+0

@MuhammadSufiyan私の作業コードは以下のものと同じです。回答、あなたのコードをここで共有することができます。問題を解決してください。 –

+0

ありがとうございます、私の問題はまた、私のコメントの同じ日に解決されました..あなたの問題を知っていることもまた解決されています。 –

答えて

1

私はあなたが2つのオプションがあると思う:あなたがダウンロードする必要がどのように多くのファイルを事前に知る、あなたがProgressDialog合計金額を設定することができます

偽のプログレスバーのアプローチ

をダウンロードするファイルの数。これは、サイズが小さくて似ているファイルではうまく機能し、何が起こっているかについてユーザーに良いフィードバックを与えます。

// you can modify the max value of a ProgressDialog, we modify it 
// to prevent unnecessary rounding math. 
// In the configuration set the max value of the ProgressDialog to an int with 
pDialog.setMax(urls.length); 

for (int i = 0; i < urls.length; i++) { 
    // launch HTTP request and save the file 
    //... 
    // your code 
    //... 

    //advance one step each completed download 
    publishProgress(); 
} 

/** 
* Updating progress bar 
*/ 
protected void onProgressUpdate(Integer... progress) { 
    pDialog.incrementProgressBy(1); 
} 

実際のプログレスバーのアプローチ

あなたは、事前にあなたがダウンロードする必要があるすべてのファイルの合計の長さを知る必要があります。たとえば、別々のファイルをダウンロードする前に、合計長さをバイト単位で指定する他のすべてのものよりも前に、別のREST APIを作成して呼び出すことができます。このようにして、すでにダウンロードした総バイト数に応じて合計ProgressDialogの長さを定期的に更新することができます。

+0

ニースの手掛かり、あなたは100/urls.lengthが暴れると言っていますが、どうすれば保護されたvoid onProgressUpdate(Integer ... progress){ //進行状況の割合を設定することができますか? pDialog.setProgress(progress [0]); } –

+0

回答を更新しました – MatPag

+0

ご返信ありがとうございます。あなたのコードを実装することによって、最初のファイルがダウンロードされた後、プログレスバーが100%にジャンプし、まだ100%にとどまります。プログレスバーが各ファイルのダウンロード後に更新/進捗していないと言うことを意味します。 –

関連する問題