Reputation: 1598
I'm trying to download a few png files using various for loops. May I ask how exactly do I employ the publishProgress() method in my case?
Here are my codes:
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;
}
As you can see, I am trying to download stuff in 2 separate for loops in the doInBackground method. However most of the examples I've seen just make the thread sleep and use the for loop counter number as the integer for the publishProgress() method. How should I be using it in this case? Thanks!
EDITED:
To further clarify, I would like to know if it's possible to have the progress update to include things done in the 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());
}
}
In postExecute(), I am trying to zip up the png files in another folder, and subsequently deleting all the png files. Is the progress bar able to include this as well? Otherwise I'll just make do with the tracking of downloading the png files.
Upvotes: 0
Views: 1151
Reputation: 4301
Write this code in 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));
}
Upvotes: 1