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

后端 未结 5 2004
后悔当初
后悔当初 2021-01-07 22:32

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

public void onCreate(Bundle savedInstanceState) { 
  super.onC         


        
相关标签:
5条回答
  • 2021-01-07 23:12

    Just for the record:

    Picasso.with(context).load(url).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Log.i(TAG, "The image was obtained correctly");
        }
    
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            Log.e(TAG, "The image was not obtained");
        }
    
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.(TAG, "Getting ready to get the image");
            //Here you should place a loading gif in the ImageView
            //while image is being obtained.
        }
    });
    

    Source: http://square.github.io/picasso/

    onPrepareLoad() is called always after starting the request. from can be "DISK", "MEMORY" or "NETWORK" to indicate where was the image obtained from.

    0 讨论(0)
  • 2021-01-07 23:19

    You cannot make synchronous requests in the main thread. If you dont want to use an AsyncThread then just use Picasso together with a Target.

    Picasso.with(Tab2.this).load(zestimateImg1).into(new Target(...);
    

    I recommend you save a reference to your target like so:

    Target mTarget =new Target (...); 
    

    This is because Picasso uses weak references to them and they might be garbage collected before the process is finished.

    0 讨论(0)
  • 2021-01-07 23:20

    try this:

    Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(new Runnable(){
        @Override
        public void run() {
            Picasso.with(Context)
                    .load(imageUrl)
                    .into(imageView);
        }
    });
    
    0 讨论(0)
  • 2021-01-07 23:35

    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

    0 讨论(0)
  • 2021-01-07 23:35

    This is the same answer of Juan José Melero Gómez but with kotlin:

    val imageBitmap = Picasso.get().load(post.path_image).into(object: Target {
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                Log.(TAG, "Getting ready to get the image");
                 //Here you should place a loading gif in the ImageView
                 //while image is being obtained.
            }
    
            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                Log.e(TAG, "The image was not obtained");
            }
    
            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                Log.i(TAG, "The image was obtained correctly");
            }
    
        })
    
    0 讨论(0)
提交回复
热议问题