【问题标题】:Picasso java.lang.IllegalStateException: Method call should not happen from the main threadPicasso java.lang.IllegalStateException:方法调用不应从主线程发生
【发布时间】:2014-11-28 01:29:32
【问题描述】:

我正在尝试使用 Picasso 从 URL 获取三张 Bitmap 图像

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

我收到了FATAL EXCEPTION 这个代码。我怀疑这与应该在AsyncTask 内完成这一事实有关,但我无法让它工作。如果可以避免使用,我想不使用AsyncTask

我怎样才能让这段代码在不崩溃的情况下运行?

如果最好的方法是使用AsyncTask,那么这个解决方案就可以了。

【问题讨论】:

  • 再次查看毕加索的文档,看看他们在哪里调用get() 以及它的用途。
  • @zapl 我发现这个:android.graphics.Bitmap get() Synchronously fulfill this request. 我不太了解线程:/
  • 我也会接受AsyncTask 解决方案,但我必须加载三个图像。我只需要这个就可以了,哈哈
  • @Brian Vanover 你能提供你的解决方案吗,实际上我正在解决同样的问题。
  • 我正在寻找一种在主线程之外执行此操作的方法,问题中的 sn-p 帮助了我。真是讽刺。

标签: android android-asynctask imageview illegalstateexception picasso


【解决方案1】:

以上都不适合我,而不是这个

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

希望对某人有用

【讨论】:

    【解决方案2】:

    您不能在主线程中发出同步请求。如果您不想使用 AsyncThread,那么只需将 Picasso 与 Target 一起使用即可。

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

    我建议您像这样保存对目标的引用:

    Target mTarget =new Target (...); 
    

    这是因为 Picasso 使用了对它们的弱引用,并且它们可能在进程完成之前被垃圾回收。

    【讨论】:

    • 你能举个例子吗?
    • 毕加索的新版本中没有with
    【解决方案3】:

    仅作记录:

    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.
        }
    });
    

    来源:http://square.github.io/picasso/

    onPrepareLoad() 总是在启动请求后调用。 from 可以是“DISK”、“MEMORY”或“NETWORK”,表示从哪里获取图像。

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

        【解决方案5】:

        这与 answer of Juan José Melero Gómez 相同,但使用 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");
                }
        
            })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多