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

时间:2016-02-26 18:11:34

标签: android multithreading android-glide

是否有人使用Glide从后台线程中获取图片?我不断得到这个断言:

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

但根据这个帖子,它应该有效:

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

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

和mContext只是活动“this”指针。

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

6 个答案:

答案 0 :(得分:8)

into(ImageView)的{​​{1}}方法要求您仅在主线程上调用它,但是当您将加载传递给Timer时,它将在Glide线程中执行。

您可以通过调用background而不是get()来检索位图,然后通过调用into()bitmap上设置ImageView

setImageBitmap()

您还可以查看此document以获取更多信息。

答案 1 :(得分:6)

发布代码以防万一。

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

答案 2 :(得分:0)

在我的情况下,我想显示来自FirebaseMessagingService的通知,其中包含将通过Glide下载的图像,这段代码给我带来了成功:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这是Kotlin-way解决方案

Glide.with(context).asBitmap().load(photo_url).into(
      BitmapImageViewTarget(imgYourResourceID)
)

答案 5 :(得分:0)

我需要在recyclerView中解密并显示大图像 而且因为这是一个繁重的过程,所以我必须在后台进行,所以这就是为什么:

 public void decryptImage(Uri uri, ViewHolder holder) {

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {

                Glide.with(G.context)
                        .asBitmap()
                        .load(decrypt(G.getEncryptionKey(), G.getConstantKey(), uri.getPath()))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                Glide.with(G.context).load(resource).into(holder.mImageView);
                            }

                        });
    
        }
    };
    new Thread(runnable).start();


}