I have used Glide many, many times before, but currently I am having issues to display images with this type of url https://www.publicdomainpictures.net/pictures/10000/nahled/thinking-monkey-11282237747K8xB.jpg
, I don't know why is impossible for me to load it. I even simplified my project to only display this image on MainActivity. When I check the Listener of glide it prompts this error
2021-02-19 21:39:43.438 31240-31240/com.y4kuzabanzai.testforvass E/MainActivity: onLoadFailed: com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 root cause:
com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: 503)
call GlideException#logRootCauses(String) for more detail
The thing is that I have tried with other Urls and they work just fine. What can be wrong? Here my simple code
MainActivity.class
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var imageView: ImageView = findViewById(R.id.homeImage)
Glide.with(this)
.load("https://www.publicdomainpictures.net/pictures/10000/nahled/thinking-monkey-11282237747K8xB.jpg")
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean
): Boolean {
Log.e(TAG, "onLoadFailed: ${e}")
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean
): Boolean {
return true
}
})
.into(imageView)
}
activity_main.layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
1. <ImageView
android:id="@+id/homeImage"
android:layout_width="418dp"
android:layout_height="492dp"
android:layout_marginStart="14dp"
android:layout_marginBottom="109dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:srcCompat="@tools:sample/avatars" /> </androidx.constraintlayout.widget.ConstraintLayout>
I finally fixed it!!!!! The thing is that you have to use a header in such cases where you have to add a User-Agent. Here how I fixed it, note that since I tried several things my code may have changed a bit, thanks Faramarz Afzali for the time. Here my code:
private val USER_AGENT = "Mozilla/5.0 (Linux; Android 11) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36"
val glideUrl = GlideUrl(
gnome.thumbnail,
LazyHeaders.Builder().addHeader("User-Agent", USER_AGENT).build())
val requestOptions = RequestOptions()
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
GlideApp.with(itemView.context)
.applyDefaultRequestOptions(requestOptions)
.load(glideUrl)
.timeout(60000)
.override(320, 480)
.into(binding.gnomeImage)