1

I am using glide and once the image is retrieved it is put into a view, the view is extended and I use the below code in the constructor. The code returns null. Is there a way to wait until it returns the drawable? It works in the listeners and that's why I am asking if there's a way to wait so it can be in the constructors.

Code:

Glide:

Glide
        .with(activity).asBitmap()
        .load(imageURL)
        .into(new SimpleTarget<Bitmap>(currentMap.getWidth(), currentMap.getHeight()) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                currentMap.setUpMap()
                });

            }

Drawable in view:

drawable = this.getDrawable();

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference

Zoe
  • 27,060
  • 21
  • 118
  • 148
Darren
  • 74
  • 1
  • 10

1 Answers1

0

Use a RequestListener<String, GlideDrawable> that gives you a drawable:

(GlideDrawable is also a Drawable).

Glide.with(activity)
    .load("...")
    .listener(object : RequestListener<String, GlideDrawable> {
        override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {

            // GlideDrawable extends Drawable :)
        }

        override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean = false
    })

You will need to remove the asBitmap() call. My code is kotlin because I copy-pasted it from my project, but I believe you can understand it.

Minas Mina
  • 2,058
  • 3
  • 21
  • 35