【问题标题】:How to calculate progress of AsyncTask which downloads multiple files如何计算下载多个文件的 AsyncTask 的进度
【发布时间】:2023-03-20 03:00:02
【问题描述】:

我正在尝试使用各种 for 循环下载一些 png 文件。请问在我的情况下,我究竟如何使用 publishProgress() 方法?

这是我的代码:

protected String doInBackground(String... params) {


    for(PlaceDetails place : places){
        for(int v = 14; v <=16; v++){   
            for (int y = 0; y <= placeBottomLeft.getYTile(); y++){
                for(int x = placeTopLeft.getXTile(); x <= placeBottomRight.getXTile(); x++){
                    try {
                        //Download some stuff here
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }

    for (int w = zoom -1 ; w <= zoom+1; w++){
        for (int y = topLeft.getYTile(); y <= bottomLeft.getYTile(); y++){
            for(int x = topLeft.getXTile(); x <= bottomRight.getXTile(); x++){
                try {
                    //Download some stuff here again
                } catch (Exception e) {
                    Log.e("URL::: ERROR", e.getMessage());
                    e.printStackTrace();
                }
            }

        }
    }
    return null;
}

如您所见,我试图在 doInBackground 方法中的 2 个单独的 for 循环中下载内容。然而,我见过的大多数示例只是让线程休眠并使用 for 循环计数器编号作为 publishProgress() 方法的整数。在这种情况下我应该如何使用它?谢谢!

编辑:

为了进一步澄清,我想知道是否有可能让进度更新包括在 onPostExecuteMethod() 中完成的事情。

protected void onPostExecute (String result){
    File newFileDir = new File(Environment.getExternalStorageDirectory().toString() + "/Qiito Offline/offlineMap");
    File fileDir = new File(Environment.getExternalStorageDirectory().toString() 
            + "/test");
    try {
        Log.e("Starting :: ", newFileDir.getPath());
        File newFile = new File(newFileDir, "" + tID + ".zip");
        newFileDir.mkdirs();
        OutputStream output = new FileOutputStream(newFile);
        ZipOutputStream zipoutput = new ZipOutputStream(output);
        zip(fileDir, fileDir, zipoutput);
        zipoutput.close();
        output.close();
        deleteDirectory(fileDir);
        mNotificationHelper.completed(); //method I used for my NotificationHelper to inform that the entire process is completed
    } catch (IOException excep) {
        Log.e("ERROR!!" , excep.getLocalizedMessage());
    }
}

在 postExecute() 中,我试图将 png 文件压缩到另一个文件夹中,然后删除所有 png 文件。进度条也能包含这个吗?否则,我将只负责下载 png 文件的跟踪。

【问题讨论】:

  • 您是要同时显示所有文件的进度,还是分别显示不同文件的进度?
  • 如果可能的话,我想展示它从 onPreExecute() 开始一直到在 onPostExecute() 完成的进度。

标签: android android-asynctask android-progressbar


【解决方案1】:

把这段代码写在doInBackground()

int totalCount  = 0; // total images count
int counter = 0; // current downloaded files count

// images - ArrayList with images
totalCount = images.size();

// download and publish progress
   int imagesCount = images.size(); 
   for (int i = 0; i < imagesCount; i++) {
            // download image

            publishProgress((int) ((counter++ / (float) totalCount) * 100));
    }

【讨论】:

  • 谢谢!问题是,图像的数量是动态的,因此我不会说出来。每个 for 循环的计数器不是一个固定的数字...
  • 你能计算循环前的所有图像数量吗?
  • 我想这可能是可能的,但我在想是否有更好的方法,因为事先计算需要遍历所有循环两次,我不确定它是否会减慢整个过程.我还更新了我的问题,想知道是否也可以计算 PostExecute 过程,这会使事情复杂化吗?
  • 或者你可以只发布步骤算作进度:例如循环计数,并在每次循环后按计算的 % 更新进度。
  • 下载图片后,你可以启动另一个AsyncTask,它会压缩和删除png文件,你可以很容易地显示进度,因为你可以得到下载的图片数量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
  • 2016-11-16
  • 2011-10-29
相关资源
最近更新 更多