3

I'm using glide to load an image using its resource ID into a bitmap, and I want to pass the bitmap to a member function of a custom view class.

This is my code:

try {
         Bitmap bm=Glide.
                with(getApplicationContext()).
                load(mThumbIds[position]).
                asBitmap().
                into(width, height). // Width and height
                get();
        drawView.setImg(bm);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

Whenever I try to run it I get the following error:

Unable to start activity ComponentInfo{...}: java.lang.IllegalArgumentException: YOu must call this method on a background thread

How do I fix this?

Talha
  • 903
  • 8
  • 31
  • Possible duplicate of [How does one use glide to download an image into a bitmap?](http://stackoverflow.com/questions/27394016/how-does-one-use-glide-to-download-an-image-into-a-bitmap) – Mike M. Jul 30 '16 at 05:26

1 Answers1

0

You can't call get() on the main thread because it performs a long running task and is likely to cause performance problems.

Instead, consider subclassing ImageViewTarget, or ViewTarget and using into() instead of get(). into() will load the image asynchronously and you can use your custom subclass to call the appropriate member function in onReasourceReady.

Be sure to clear out your Bitmap (usually by passing null to the member function) when onLoadFailed or onLoadCleared are called.

Sam Judd
  • 7,317
  • 1
  • 38
  • 38