【问题标题】:progress dialog for downloading pics下载图片的进度对话框
【发布时间】:2011-12-14 13:53:07
【问题描述】:

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

【问题讨论】:

  • 请具体说明您面临的问题
  • 我有进度对话框出现,但它只在下载一张图片时保持可见,使其无效
  • 那你做错了给我们看代码
  • 这是您的解决方案检查this 一个。

标签: android progress


【解决方案1】:

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

希望你得到它

【讨论】:

    【解决方案2】:

    试试这个 ::

    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();
            }
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多