下载图片的进度对话框

发布于 2024-12-20 21:45:24 字数 1306 浏览 2 评论 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);
    }
}

I am downloading roughly 231 pics and would like to have a progress dialog say downloading pics please wait until all the pics are available and then disappear. How would I go about doing this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-12-27 21:45:25

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

希望你能明白

I think u have dont wrong thing,
show progress dialog in onpreExecute() , ur logic in doinBackground() and dismiss progress dialog in onPostExecute().

Hope u get it

纵山崖 2024-12-27 21:45:25

试试这个::

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

try this ::

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();
        }
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文