picasso: Crashes using Picasso in Intent Service

Just saw a bunch of these crashes in my logs after updating to 2.3.2. Never had an issue with older versions.

java.lang.IllegalStateException: Method call should happen from the main thread.
       at com.squareup.picasso.Utils.checkMain(Utils.java:122)
       at com.squareup.picasso.RequestCreator.into(RequestCreator.java:351)
       at com.cluster.services.GcmIntentService.getPhoto(GcmIntentService.java:225)
       at com.cluster.services.GcmIntentService.sendNotification(GcmIntentService.java:95)
       at com.cluster.services.GcmIntentService.onHandleIntent(GcmIntentService.java:65)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.os.HandlerThread.run(HandlerThread.java:61)

private void getPhoto(String url, final int position) {
        Picasso.with(getApplicationContext()).load(url).into(new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
            }

            @Override
            public void onBitmapLoaded(Bitmap bm, LoadedFrom arg1) {
                checkPhoto(bm, position);
            }

            @Override
            public void onBitmapFailed(Drawable arg0) {
            }
        });
    }

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 17 (1 by maintainers)

Most upvoted comments

Quick workaround is performing the method call on a Handler on the main Looper.

 Handler uiHandler = new Handler(Looper.getMainLooper());
 uiHandler.post(new Runnable(){
            @Override
            public void run() {
                Picasso.with(mContext).load(formatStaticImageUrlWith(location)).transform(new StaticMapTransformation()).into(remoteView, R.id.static_map_image, Constants.NOTIFICATION_ID_NEW_TRIP_OFFER, notification);
            }
        });

I wanted to mention that @dnkoutso recommendation of using the get() worked great on a bg thread. I used it for the large icon bitmap on a notification.