0

I'm trying to use Picasso inside a Java thread (I know it's not necessary to run Picasso in a thread, but my architecture demands it at one point).

Here's my code:

    @Override
    public void run() {
        Log.d(LOG_TAG, "Run " + source);
        final Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d(LOG_TAG, "onBitmapLoaded");
                final Bitmap bm = bitmap;
                mMainThread.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onImageDownloaded(bm);
                    }
                });
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                Log.d(LOG_TAG, "FAILED");
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                Log.d(LOG_TAG, "Prepare Load");
            }
        };
        Picasso.with(context).load(source).resize(width, height).into(target);
    }

The run function is executed using Android's ThreadPoolExecutor. The function is called, but none of the callbacks (onBitmapLoaded, onBitmapFailed, onPrepareLoad) are ever called.

What am i doing wrong?

Irshu
  • 8,248
  • 8
  • 53
  • 65
Androidicus
  • 1,688
  • 4
  • 24
  • 36
  • Try removing final from the target object, also moving the creation of the target object in line with into. e.g. `.into(new Target() {...` I say this as I'm doing it this way and it's working. I can't see anything else different so it's worth a try! – vguzzi Jun 06 '16 at 09:50
  • // Get a handler that can be used to post to the main thread Handler mainHandler = new Handler(context.getMainLooper()); Runnable myRunnable = new Runnable() { @Override public void run() {....} // This is your code }; mainHandler.post(myRunnable); – vguzzi Jun 06 '16 at 10:09
  • He marked your question as duplicate, but the duplicate answer is wrong, your code is breaking as it's called in the background thread. I hope it fixes it for you. (see code above) – vguzzi Jun 06 '16 at 10:10

1 Answers1

-1

Use below code snippet:

 Picasso.with(getContext()).load(imageUrl).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        //Add your own logic
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
      //Add your own logic
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
      //Add your own logic                             
    }
});
David
  • 15,894
  • 22
  • 55
  • 66