java - 更新进度对话框

标签 java android dialog progress

我正在尝试制作一个应用程序来帮助我评估从 Web 资源下载文件的时间。我找到了 2 个样本:

Download a file with Android, and showing the progress in a ProgressDialog

http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

第二个示例显示了更短的下载时间,但我无法理解如何使用它来更新进度对话框。我认为在第二种情况下应该用“while”表达式来完成一些事情,但我找不到什么。有人可以给我任何建议吗?

更新:

第一个代码:

try {
            time1 = System.currentTimeMillis();
            URL url = new URL(path);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conexion.getContentLength();
            // downlod the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/analyzer/test.jpg");

            byte data[] = new byte[1024];

            long total = 0;

          time11 = System.currentTimeMillis();
           while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int)(total*100/lenghtOfFile));
                output.write(data, 0, count);
            }
            time22= System.currentTimeMillis()-time11;
            output.flush();
            output.close();
            input.close();


        } catch (Exception e) {}

        timetaken = System.currentTimeMillis() - time1;

第二个代码:

       long time1 = System.currentTimeMillis();
        DownloadFromUrl(path, "test.jpg");
        long timetaken = System.currentTimeMillis() - time1;

在哪里

  public void DownloadFromUrl(String imageURL, String fileName) {  //this is the downloader method
 try {
         URL url = new URL(imageURL); //you can write here any link
         File file = new File(fileName);

        /*Open a connection to that URL. */
         URLConnection ucon = url.openConnection();

         /*
          * Define InputStreams to read from the URLConnection.
          */
         InputStream is = ucon.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);

         /*
          * Read bytes to the Buffer until there is nothing more to read(-1).
          */
         ByteArrayBuffer baf = new ByteArrayBuffer(50);
         int current = 0;
         while ((current = bis.read()) != -1) {
                 baf.append((byte) current);
         }

         /* Convert the Bytes read to a String. */
         FileOutputStream fos = new FileOutputStream(PATH+file);
         fos.write(baf.toByteArray());
         fos.close();

 } catch (IOException e) {
         Log.d("ImageManager", "Error: " + e);
 }

所以问题是第一种方法似乎慢了大约 30%。

最佳答案

second example可能运行得更快,但它独占了 GUI 线程。 first approach , 使用 AsyncTask , 更好;它允许 GUI 在下载过程中保持响应。

我发现比较 AsyncTask 很有帮助与 SwingWorker ,如图所示 example .

关于java - 更新进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524472/

相关文章:

java - Java 中的预自增和后自增行为

java - 如何编写一个构造函数来阻止在某些条件下创建对象

java - 如何在Java中自动启动Quartz Scheduler

安卓 NFC : Exception "transceive failed" when using transceive()

android - 无法解析 GoogleSignIn 和 GoogleSignInClient

javascript - Vue 警告和对话框只出现一次

c++ - 为什么我的程序不能显示这个对话框,而另一个程序可以?

java - java中有效过滤字符串

android - 在Android中画一条线而不将其设置为内容

javascript - 我怎样才能让 AngularJS 绑定(bind)与花式框对话框一起工作?