【问题标题】:Updating progress dialog更新进度对话框
【发布时间】:2011-04-02 16:45:33
【问题描述】:

我正在尝试制作一个可以帮助我评估从网络资源下载文件的时间的应用程序。我找到了 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%。

【问题讨论】:

    标签: java android dialog progress


    【解决方案1】:

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

    我发现将AsyncTaskSwingWorker 进行比较很有帮助,如example 所示。

    【讨论】:

    • 我在 Asynctask 中执行第二个方法。
    • 嗯,第一个代码有一个 1024 字节的缓冲区,而第二个只有 50 个。
    • 我尝试将缓冲区减少到 50。但我没有看到性能有任何变化。
    • 你说,“我下载文件的第一个代码慢了大约 30%”和“第一个方法似乎快了大约 30%。”这似乎是一个矛盾。我注意到AsyncTask 必须将其操作与 GUI 交错显示进度,所以 30% 似乎是合理的。
    • 抱歉,我更正了错误。是否可以通过更新进度对话框向 Asynctask 插入第二个代码?或者在这种情况下,我的性能也会下降?或者我可以提高第一个代码的性能?
    【解决方案2】:

    第一个链接最好。但我无法在周一或之后提供代码(这是家庭组合),我可以提供完整的功能。但是:

    private class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... url) {
            int count;
            try {
                URL url = new URL(url[0]);
                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/somewhere/nameofthefile.ext");
    
                byte data[] = new byte[1024];
    
                long total = 0;
    
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    publishProgress((int)(total*100/lenghtOfFile));
                    output.write(data, 0, count);
                }
    
                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {}
            return null;
        }
    

    这门课最适合它(恕我直言)。 publishProgress 这是一个简单的函数,你最多有两行。设置最大值和设置电流。您如何在此代码中看到lenghtOfFile 这是您的文件有多少字节。 total-当前进度(示例 25 来自 100 个字节)。轻松上这门课:DownloadFile a = new DownloadFile(); a.execute(value,value);//or null if u not using value.希望你能理解我,我英语不好。

    【讨论】:

    • 我了解它的工作原理,但我发现使用第一个代码下载文件比使用第二个代码慢大约 30%。我不明白为什么。
    • 字节数据[] = 新字节[1024];您需要在此处设置新值,例如 16000 。而且它的下载速度增加了
    猜你喜欢
    • 2019-08-08
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多