我之前已经使用过很多次Glide,但是目前我在使用这种类型的url显示图像时遇到问题https://www.publicdomainpictures.net/pictures/10000/nahled/thinking-monkey-11282237747K8xB.jpg
,我不知道为什么我无法加载它。我什至简化了我的项目,使其仅在MainActivity上显示此图像。当我检查滑行监听器时,会提示此错误
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
事实是,我尝试过与其他Urls一起使用,并且效果很好。有什么事吗 这是我的简单代码
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>
我终于修好了!!!!! 事实是,在必须添加用户代理的情况下,必须使用标头。在这里,我如何修复它,请注意,由于尝试了几件事,我的代码可能有所更改,在此感谢Faramarz Afzali。这是我的代码:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句