Android 如何计算下载多个文件的AsyncTask的进度

Android 如何计算下载多个文件的AsyncTask的进度,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我正在尝试使用各种for循环下载一些png文件。我可以问一下,在我的案例中,我到底是如何使用publishProgress()方法的吗 这是我的密码: protected String doInBackground(String... params) { for(PlaceDetails place : places){ for(int v = 14; v <=16; v++){ for (int y = 0; y <= pl

我正在尝试使用各种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;
}

在postExecute()中,我试图将png文件压缩到另一个文件夹中,然后删除所有png文件。进度条是否也可以包含此内容?否则,我将只跟踪下载png文件。

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));
    }
int totalCount=0;//图像总数
int计数器=0;//当前下载的文件数
//图像-带有图像的ArrayList
totalCount=images.size();
//下载并发布进度
int-ImageScont=images.size();
对于(int i=0;i
您想同时显示所有文件的进度还是分别显示不同文件的进度?如果可能,我想显示从onPreExecute()开始到onPostExecute()完成的进度。谢谢!问题是,图像的数量是动态的,因此我不想说。每个for循环的计数器不是一个固定的数字…你能在循环之前计算所有图像的计数吗?我想这可能是可能的,但我在想是否有更好的方法,因为手动计算需要遍历所有循环两次,我不确定这是否会减慢整个过程。我还更新了我的问题,想知道是否也可以计算PostExecute过程,这会使问题复杂化吗?或者你可以只发布步骤作为进度:例如循环计数,每次循环更新后的进度按计算的百分比计算。下载图像后,你可以启动另一个AsyncTask,它将压缩和删除png文件,在那里你可以很容易地显示进度,因为你可以下载图片
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));
    }