2011-12-14 71 views
0

我下載大約231圖片,並希望有一個進展對話說下載圖片,請等待,直到所有的圖片可用,然後消失。我會如何去做這件事?進度對話框下載圖片

protected class DownloadFile extends AsyncTask<String, Integer, String>{ 
    @Override 
    protected String doInBackground(String... url) 
    { 
     int count; 
     try 
     { 
      URL url1 = new URL(url[0]); 
      URLConnection conexion = url1.openConnection(); 
      conexion.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int lenghtOfFile = conexion.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(url1.openStream()); 
      byte data[] = new byte[1024]; 

      long total = 0; 
      while ((count = input.read(data)) != -1) 
      { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int)(total*100/lenghtOfFile)); 
      } 
      input.close(); 
     } 
     catch (Exception e) 
     { 
     } 
     return null; 
    } 
    public void onProgressUpdate(Integer... values) 
    { 
     super.onProgressUpdate(values); 
     // here you will have to update the progressbar 
     // with something like 
     setProgress(numPokemon); 
    } 
} 
+0

請具體到你所面對 – ingsaurabh 2011-12-14 05:21:39

回答

0

我覺得你有不說錯話,在onpreExecute 顯示進度對話框(),烏爾在doinBackground邏輯(),並駁回onPostExecute進度對話框()。

希望你得到它

0

嘗試這種::

class AddTask extends AsyncTask<Void, Void, Void> { 
     ProgressDialog pDialog = ProgressDialog.show(Recording.this,"Please wait...", "Retrieving data ...", true); 
     protected void onPreExecute() { 

      pDialog.setIndeterminate(true); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
      SaxParser(Username,Password); 

     } 

     protected Void doInBackground(Void... unused) { 
      // your code 
      return(null); 
     } 
     protected void onPostExecute(Void unused) { 
      pDialog.dismiss(); 
     } 
     }