Glide断言:java.lang.IllegalArgumentException:您必须在主线程上调用此方法

gmm*_*mmo 14 multithreading android android-glide

有没有人用Glide从后台线程中获取图像?我一直得到这个断言:

java.lang.IllegalArgumentException: You must call this method on the main thread
Run Code Online (Sandbox Code Playgroud)

但根据这个线程,它应该工作:

https://github.com/bumptech/glide/issues/310

然而,我不能让它工作,除非我从主线程中调用它.

这是我试图从主线程做的事情:

    Glide.get(mContext);
    loadUserImage(userImageUrl);

    // wait 5 seconds before trying again
    int imageLoadingTimeOut = mContext.getResources().getInteger(R.integer.image_loading_time_out);
    if (imageLoadingTimeOut > 0) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if (!mUserImageLoaded) {
                    loadUserImage(userImageUrl);
                }
            }
        }, imageLoadingTimeOut);
    }
}
Run Code Online (Sandbox Code Playgroud)

和loadUserImage:

private boolean mUserImageLoaded = false;

private void loadUserImage(String userImageUrl) {

    if (userImageUrl != null && !userImageUrl.isEmpty() && !mUserImageLoaded) {
        Glide.with(mContext).using(Cloudinary.getUrlLoader(mContext)).load(userImageUrl).crossFade().listener(new RequestListener<String, GlideDrawable>() {

            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                mImageMessageContent.invalidate();
                mUserImageLoaded = true;
                return false;
            }
        }).into(mImageMessageContent);

    } else {
        mImageMessageContent.setVisibility(View.GONE);
    }
}
Run Code Online (Sandbox Code Playgroud)

和mContext只是活动"this"指针.

无论如何,我可以使用不同于主线程的Glide吗?

Avs*_*tor 8

into(ImageView)方法Glide要求您仅在主线程上调用它,但是当您将加载传递给Timer时,它将在一个background线程中执行.

你可以做的是通过调用来检索位图get()来代替into(),然后设置bitmapImageView调用setImageBitmap().

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

您还可以查看此文档以获取更多信息.


fre*_*yle 7

发布代码以防万一它可以帮助某人.

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


Raj*_*shi 6

更新主ui线程中的图像

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