0

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();
    }
}
baeharam
  • 543
  • 1
  • 8
  • 20
  • @Nilesh Rathod It is not duplicate, that post is about toast in doinbackground! My post is about onprogressupdate plz uncheck duplicate mark. – baeharam Aug 17 '18 at 08:15
  • Why you want to display toast in on progress update, check this use of **`onProgressUpdate`** https://stackoverflow.com/questions/2837676/how-to-raise-a-toast-in-asynctask-i-am-prompted-to-used-the-looper – AskNilesh Aug 17 '18 at 08:17
  • What r u talking about, `onProgressUpdate()` is exist to see how working is progressed. So, I just want to use Toast to see! Why u unvoted? – baeharam Aug 17 '18 at 08:23
  • Thats not my down vote and have look **[onProgressUpdate](https://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...))** – AskNilesh Aug 17 '18 at 08:24
  • Here ya go: [getting context in AsyncTask](https://stackoverflow.com/questions/16920942/getting-context-in-asynctask). – Mike M. Aug 17 '18 at 08:25
  • Document said "Progress: The values indicating progress.". IT IS THE RIGHT WAY HOW TO USE `onProgressUpdate()`. – baeharam Aug 17 '18 at 08:26
  • @MikeM. Oh THX!!! – baeharam Aug 17 '18 at 08:26

0 Answers0