2011-12-14 70 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(); 
     } 
     }