Picasso java.lang.IllegalStateException: Method call should not happen from the main thread

Brian picture Brian · Nov 28, 2014 · Viewed 20k times · Source

I am attempting to use Picasso to get three Bitmap images from a URL

public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab2);
  Drawable d1 = new BitmapDrawable(Picasso.with(Tab2.this).load(zestimateImg1).get());
}

I am getting FATAL EXCEPTION with this code. I suspect it has to do with the fact that this should be done within AsyncTask, but I can't get it to work. If using that is avoidable, I would like to do this without using AsyncTask.

How can I get this code to run without crashing?

If the best way to do this is with AsyncTask, then that solution is ok.

Answer

Nikhil picture Nikhil · Aug 2, 2017

None of above worked for me instead this

Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(new Runnable(){
        @Override
        public void run() {
            Picasso.with(Context)
                    .load(imageUrl)
                    .into(imageView);
        }
    });

Hope it may be useful for someone