Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dunkindonass opened this issue May 2, 2024 · 1 comment

Comments

@dunkindonass
Copy link

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?

@eyeahs
Copy link

eyeahs commented May 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants