0

I'm trying to display a custom dialog while bitmap saving in Gallery, I tried to use Asynthask and display the custom dialog in onPreExecute method and dismiss it in onPostExecute method.

@SuppressLint("StaticFieldLeak")
    public class saveWithStickersFile extends AsyncTask<File, File, File> {
        Context mContext;
        TextView view;

        saveWithStickersFile(Context context) {
            mContext = context;
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            content.setVisibility(View.GONE);
            dialogLoading = new Dialog(BaseActivity.this);
            dialogLoading.requestWindowFeature(Window.FEATURE_NO_TITLE);
            assert dialogLoading.getWindow() != null;dialogLoading.getWindow().setBackgroundDrawableResource(R.color.transparent);
            dialogLoading.setCancelable(true);
            dialogLoading.setContentView(R.layout.bottomdialog);
            GifImageView gifImageView = dialogLoading.findViewById(R.id.progress);
            gifImageView.setVisibility(View.VISIBLE);
            LinearLayout buttons = dialogLoading.findViewById(R.id.buttons);
            buttons.setVisibility(View.GONE);
            view = dialogLoading.findViewById(R.id.text);
            view.setText("Saving ...");
            fullImageView.setVisibility(View.VISIBLE);
            dialogLoading.show();
        }

        @Override
        protected File doInBackground(File... files) {
            File fileSaved = files[1];
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    stickerView.save(files[0], true);
                }
            });
            return fileSaved;
        }


        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            if (file.exists()) {
                dialogLoading.dismiss();
                Toast.makeText(BaseActivity.this, "Done !", Toast.LENGTH_SHORT).show();
            }
        }
    }

My Issue:

When I run the app, Bitmap saved in the gallery but I can't see the custom dialog, and nothing appears, for some reason, runOnUiThread prevents the onPreExecute method. can anyone help me to get resolve this issue?

  • 1
    `runOnUiThread()` is completely defeating the purpose of the `AsyncTask`. The whole reason to use an `AsyncTask` here is to move that operation off the UI thread. As you have it now, that save is blocking the UI thread, which is why you won't see the `Dialog`. Why did you put that in a `runOnUiThread()` to begin with? Were you getting an error when you didn't? If so, what was that? – Mike M. Aug 03 '19 at 10:23
  • @MikeM. thank you so much for your comment, When I remove `runOnUiThread` I get this error message: `Method save must be called from the UI thread, currently inferred thread is worker thread` – Mouaad Abdelghafour AITALI Aug 03 '19 at 11:38
  • 1
    What is `stickerView`, exactly? Is that a library `View`? If so, could you give us a link, please? – Mike M. Aug 03 '19 at 11:46
  • @MikeM. yes it's a `view` extends from `FrameLayout` , if you have a time check it from here : [StickerView.java](https://github.com/wuapnjie/StickerView/blob/master/sticker/src/main/java/com/xiaopo/flying/sticker/StickerView.java) – Mouaad Abdelghafour AITALI Aug 03 '19 at 11:49
  • Hmm, the `save()` method shown in that source takes only a single `File` argument. Your snippet shows a call with a `File` and a `boolean`. Have you modified that somehow? – Mike M. Aug 03 '19 at 11:55
  • @MikeM. yes, I've modified if the value of `boolean` **true**,notifyGallery (show saved `bitmap` in gallery). I've created a gist please check it [StickerView.java](https://gist.github.com/Mouadabdelghafouraitali/aa57f7872259b7d9b42da31196922684) – Mouaad Abdelghafour AITALI Aug 03 '19 at 12:00
  • Did you change anything else there? AFAIK, that error is coming from a `@UiThread` annotation, and I don't see any such annotation in that class, or in any of the helper methods. – Mike M. Aug 03 '19 at 12:08
  • OK, yeah, I don't see anything in your modified class, either. The only thing I can think of is perhaps one of the native `View` method calls is causing that? You might try putting your cursor on the error line, hitting alt-enter, and choosing the "Suppress..." option, if there is one. Then try a test run, and see if anything goes wrong. – Mike M. Aug 03 '19 at 12:13
  • @MikeM. thank you, and sorry for disturbing you, I added @SuppressLint("WrongThread") above `doInBackground`, and it's worked, can you please explain to me what this line means – Mouaad Abdelghafour AITALI Aug 03 '19 at 12:21
  • Well, as I mentioned, that error (warning?) you were getting is due to a `@UiThread` annotation somewhere. (AFAIK, that's the only thing that'll give that specific message.) That annotation is a hint to the build tools, so they can try to help you prevent runtime problems when calling certain methods on the wrong thread. For example, trying to update a `View` from your `doInBackground()` method would throw an `Exception` at runtime. That particular `@SuppressLint` annotation basically overrides the `@UiThread` annotation, telling the build tools to just ignore it. – Mike M. Aug 03 '19 at 12:32
  • The only snag here is, I'm not sure what's causing the original error/warning in the first place. If I get a chance later, I'll try to test that out, and pin down what's causing it. If you're able to run without any problems, though, I would think you should be OK, but you might want to test that whole thing pretty thoroughly. – Mike M. Aug 03 '19 at 12:32

0 Answers0