0

I am using picasso to save image to disk on tap of a button by the user and I want to generate a feedback in the form of a Toast to the user that the image has been downloaded.

For this, I am trying to run a toast on the UI Thread by using the following code::

((AppCompatActivity)context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                }
});

Which is not running. The image is downloaded and is also visible in the gallery of my app, but the Toast Does not show up. Can someone tell me if I am actually doing this right or should it be done some other way?

FYI: This code is being run in the onBitmapLoaded() method of Target object that I am passing to Picasso to download the Image into; The 'context' object here refers to the current Activity's context.

Any help would be appreciated :)

Divij Sehgal
  • 647
  • 2
  • 11
  • 26

2 Answers2

1

After doing a lot of search on net I got the answer. You need to display toast on main thread not on background thread. Following code will do the work

Handler handler = new Handler(Looper.getMainLooper());

                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            //Display toast here


                        }
                    });
AZIM MOHAMAD
  • 72
  • 11
0

Use the method that has a callback, there you can define a message for success and error.

final ImageView view = new ImageView(this);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(view, new Callback() {
        @Override
        public void onSuccess() {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError() {

        }
    });

Edit: Add line of the placeholder

  • I am sorry but the issue with this is that this method works only with an imageView as the target. – Divij Sehgal Nov 07 '16 at 14:16
  • If the **image download target** is a Custom Target object, it is not possible to use a custom CallBack as the second argument as there us not such method available. :| – Divij Sehgal Nov 07 '16 at 14:17
  • I am using the imageview as a temporary placeholder. – Henrique César Madeira Nov 10 '16 at 11:29
  • So where do you put the logic to save the image to disk? And do you fetch the image from the Cache and save it to the disk or do you make a separate call? Would like to see your logic if possible. Thanks :) – Divij Sehgal Nov 10 '16 at 14:55