Issue
I am trying to update the progress bar after creation (both in AsyncTask). The progress dialog shows up and dismisses but not updated, it is always set to 0. Here is my code: public class ImageTask extends AsyncTask {
@Override
protected Void doInBackground(String... saleId) {
//Drawable drawable = getActivity().getResources().getDrawable(R.drawable.sale_image_holder);
//Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
//ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//byte[] imageInAsByteArray = stream.toByteArray(); // here
ImgService imgService = new ImgService();
imgService.addImgToSale(imageInAsByteArray, saleId[0], this);
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
public void doProgress(int value){
publishProgress(value);
}
}
here is the ImgService class:
public class ImgService
{
public void addImgToSale(byte[] img, final String saleId, final ImageTask task)
{
final ParseFile file = new ParseFile(saleId + ".jpeg", img);
try {
file.save();
} catch (ParseException e1) {
e1.printStackTrace();
return;
}
task.doProgress(50);
ParseObject image = new ParseObject(DBDictionary.SALE_IMAGES);
ParseObject sale = new ParseObject(DBDictionary.SALE);
sale.setObjectId(saleId);
image.put(DBDictionary.SALE_IMAGES_SALE_ID_P, ParseObject.createWithoutData(DBDictionary.SALE, saleId));
image.put("image", file);
try {
image.save();
} catch (ParseException e1) {
e1.printStackTrace();
return;
}
task.doProgress(100);
}
}
In the net I found many problems with showing and dismissing the ProgressDialog but not with the progress updating.
Thanks!
Solution
From android doc: use onProgressUpdate function inside your asynctask.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));//this function updates the progress bar ...
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Answered By - amalBit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.