Glide assert: java.lang.IllegalArgumentException: You must call this method on the main thread

泪湿孤枕 提交于 2019-12-01 15:56:31
Avsector

The into(ImageView) method of Glide requires you to call it only on main thread, but when you pass the loading to a Timer it will be executed in a background thread.

What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap().

Glide.with(getApplicationContext())
     .load("your url")
     .asBitmap()
     .into(new BitmapImageViewTarget(imgView) {
      @Override
      protected void setResource(Bitmap resource) {
       //Play with bitmap
        super.setResource(resource);
      }
    });

You can also take a look at this document for more information.

Posting the code just in case it helps someone.

Bitmap myBitmap = Glide.with(applicationContext)
        .load(yourUrl)
        .asBitmap()
        .centerCrop()
        .into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
        .get()
imageView.setImageBitmap(myBitmap);

Update image in main ui thread

runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(MainActivity.this)
                                    .load("image URL")
                                    .into(imageView);
                        }
                    });

In my case i want to show notification from FirebaseMessagingService with image which will be downloaded via Glide, what gave me success is this piece of code:

try {
                    Bitmap bitmap= Glide.with(getApplicationContext())
                            .load(imageUrl)   // image url in string
                            .asBitmap().into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();

                    // now i can pass bitmap to notificationBuilder like
                 notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)); 

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
 private class AsyncTaskRunner extends AsyncTask<String, String, RequestBuilder>
{
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(MainActivity.this,
                "Please Wait",
                "Image is loading...");
    }

    @Override
    protected RequestBuilder<Drawable> doInBackground(String... strings) {
        URL url = null;
        try {
            url = new URL(strings[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        RequestBuilder<Drawable> g= Glide.with(MainActivity.this).load(url);
        return g;
    }

    @Override
    protected void onPostExecute(RequestBuilder v) {
        v.into(imageView);
        imageView.setVisibility(View.VISIBLE);
        progressDialog.dismiss();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!