glide icon indicating copy to clipboard operation
glide copied to clipboard

Glide placeholders load drawable resources with wrong context

Open bherbst opened this issue 4 years ago • 4 comments
trafficstars

Glide Version: 4.12.0

Device/Android Version: Explicitly tested on Pixel 5 running Android 12, but this is applicable to all devices and Android versions.

Issue details / Repro steps / Use case background:

When using a placeholder resource ID like so, Glide uses an unexpected Context to load the drawable:

val options = RequestOptions().placeholder(R.color.some_color_resource)
Glide.with(imageView.context)
   .load("http://the.url")
   .apply(options)
   .into(imageView)

I would expect this to use the imageView's context to load the Placeholder (which in most cases would be an Activity), but in fact it uses the application context to load the image.

This is because SingleRequest uses glideContext to load the drawable.

The reason we discovered this is this behavior does not work well with dark mode. Specifically, if the device is currently in dark mode but an application has set itself to MODE_NIGHT_NO via AppCompatDelegate.setDefaultNightMode() then the placeholder will be incorrectly loaded from values-night.

Glide load line / GlideModule (if any) / list Adapter code (if any):

val options = RequestOptions().placeholder(R.color.some_color_resource)
Glide.with(imageView.context)
   .load("http://the.url")
   .apply(options)
   .into(imageView)

values/colors.xml:

<color name="some_color_resource">#ff000000</color>

values-night/colors.xml:

<color name="some_color_resource">#ffffffff</color>

SampleApplication.kt:

class SampleApplication : Application {
  override fun onCreate() {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  }
}

bherbst avatar Nov 19 '21 16:11 bherbst

As a short term fix we are getting the Drawable instance ourselves via ResourcesCompat.getDrawable() instead of letting Glide manage getting the drawable.

bherbst avatar Nov 19 '21 17:11 bherbst

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

stale[bot] avatar Jan 09 '22 17:01 stale[bot]

Still an issue.

bherbst avatar Jan 10 '22 14:01 bherbst

Would be great if this could be fixed

connyduck avatar Apr 26 '22 20:04 connyduck

Still waiting for a fix.

alexwyattdev avatar Oct 10 '22 10:10 alexwyattdev

Seems like a duplicate of https://github.com/bumptech/glide/issues/3778 and https://github.com/bumptech/glide/issues/3751, PR for fix in https://github.com/bumptech/glide/pull/4842

TWiStErRob avatar Oct 10 '22 11:10 TWiStErRob

Duplicate of #3751

Thanks @TWiStErRob

sjudd avatar Oct 10 '22 17:10 sjudd