用于下载图片的进度对话框

时间:2011-12-14 05:18:09

标签: android progress

我正在下载大约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);
    }
}

2 个答案:

答案 0 :(得分:0)

我认为你没有错, 在onpreExecute()中显示进度对话框,在doinBackground()中显示ur逻辑,并在onPostExecute()中关闭进度对话框。

希望你明白

答案 1 :(得分: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();
        }
      }