Search code examples
androidandroid-imageviewandroid-glide

Unable to load bitmap using Glide v4


I am using Glide v4 to load a bitmap that can then be used to as a marker on the map. When I use the deprecated SimpleTarget like so everything works fine.

GlideApp.with(getContext()).asBitmap().load(url)
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

            // load bitmap as marker
        }
    });

When I try removing the deprecated code and using Target<Bitmap> like given below I can see the onLoadStarted gets called but the onResourceReady is never called neither is the onLoadFailed.

GlideApp.with(getContext()).asBitmap()
    .load(UrlHelper.createUrl(poi.getMapMarker()))
    .into(marketBitmap);

private Target<Bitmap> marketBitmap = new Target<Bitmap>() {
    @Override
    public void onLoadStarted(@Nullable Drawable placeholder) {
        Log.d("GlideMar", "marker load started");
    }

    @Override
    public void onLoadFailed(@Nullable Drawable errorDrawable) {
        Log.e("GlideMar", "marker load failed");
    }

    @Override
    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
        Log.d("GlideMar", "onResourceReady");
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {
        Log.d("GlideMar", "marker onLoadCleared");
    }

    @Override
    public void getSize(@NonNull SizeReadyCallback cb) {

    }

    @Override
    public void removeCallback(@NonNull SizeReadyCallback cb) {

    }

    @Override
    public void setRequest(@Nullable Request request) {

    }

    @Nullable
    @Override
    public Request getRequest() {
        return null;
    }

    @Override
    public void onStart() {
        Log.d("GlideMar", "marker onStart");
    }

    @Override
    public void onStop() {
        Log.d("GlideMar", "marker onStop");
    }

    @Override
    public void onDestroy() {
        Log.d("GlideMar", "marker onDestroy");
    }
};

Solution

  • From Glide Custom Targets documentation.

    If you’re using a custom Target and you’re not loading into a View that would allow you to subclass ViewTarget, you’ll need to implement the getSize() method.

    So in your case just put the below code in getSize method

    @Override
    public void getSize(SizeReadyCallback cb) {
        cb.onSizeReady(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
    }
    

    Now the onResourceReady method will be called when you run the app.