glide icon indicating copy to clipboard operation
glide copied to clipboard

error(R.drawable.xxxx) and placeholder(R.drawable.xxxx) do not work in glide

Open dunkindonass opened this issue 9 months ago • 2 comments

Glide Version: 4.11.0

Integration libraries:

Device/Android Version: any devices

Issue details / Repro steps / Use case background: I am using Glide in dfm and save the image in the drawble folder so that an error image is displayed when the image load fails as shown below.

 Glide.with(binding.root.context)
  .load("url")
  .error(R.drawable.xxxxx)
  .into(binding.icon)

If the resource id value of R.drawable.xxxxx is a negative integer, it will not operate. If it is a positive integer, it will operate normally.

 public T error(@DrawableRes int resourceId)

Are you separately checking the validity of the errorId value saved in the above function?

dunkindonass avatar May 02 '24 06:05 dunkindonass

In the Glide library version 4.14.2, there is an issue with the method getErrorDrawable() located in the com.bumptech.glide.request.SingleRequest<R> class, specifically at line 386. The method is defined as follows:

@GuardedBy("requestLock")
private Drawable getErrorDrawable() {
  if (errorDrawable == null) {
    errorDrawable = requestOptions.getErrorPlaceholder();
    if (errorDrawable == null && requestOptions.getErrorId() > 0) {
      errorDrawable = loadDrawable(requestOptions.getErrorId());
    }
  }
  return errorDrawable;
}

The conditional check requestOptions.getErrorId() > 0 is problematic because Android resource IDs can be negative values. Thus, this code may lead to incorrect behavior by not properly handling cases where the error ID is negative. This oversight could prevent the loading of valid negative resource IDs as error drawables.


See the static method isValid(@AynRes int id) located in the android.content.res.ResourceId class.

    /**
     * Checks whether the integer {@code id} is a valid resource ID, as generated by AAPT.
     * <p>Note that a negative integer is not necessarily an invalid resource ID, and custom
     * validations that compare the {@code id} against {@code 0} are incorrect.</p>
     * @param id The integer to validate.
     * @return {@code true} if the integer is a valid resource ID.
     */
    public static boolean isValid(@AnyRes int id) {
        // With the introduction of packages with IDs > 0x7f, resource IDs can be negative when
        // represented as a signed Java int. Some legacy code assumes -1 is an invalid resource ID,
        // despite the existing documentation.
        return id != -1 && (id & 0xff000000) != 0 && (id & 0x00ff0000) != 0;
    }

eyeahs avatar May 02 '24 08:05 eyeahs

Fix PR: https://github.com/bumptech/glide/pull/5447!

injae-kim avatar Sep 09 '24 16:09 injae-kim