I coded image labeling of all the images of gallery using firebase ml kit. But it was so long task that I did dealing with AsyncTask.
I wanted to show progress of image labeling in onProgressUpdate() by Toast, but it is not working. Essentially, I have to use context but AsyncTask is static inner class and to get a context, I must send context variable to that method.
But doInBackground() is all so in inner static class. How can I pass context variable?
private static class ImageLabelTask extends AsyncTask<Void, Double, Void> {
@Override
protected Void doInBackground(Void... voids) {
for(FirebaseVisionImage image : firebaseVisionImages) {
detector.detectInImage(image).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionLabel>>() {
@Override
public void onSuccess(List<FirebaseVisionLabel> firebaseVisionLabels) {
allLabels.addAll(firebaseVisionLabels);
Double percent = ((double)allLabels.size()/(double)numOfLabel)*100.0;
DecimalFormat df = new DecimalFormat("#.##");
publishProgress(Double.parseDouble(df.format(percent)));
}
});
}
return null;
}
@Override
protected void onProgressUpdate(Context context, Double... values) {
Toast.makeText(GalleryActivity.this, String.valueOf(values[0])+"%",Toast.LENGTH_SHORT).show();
}
}