Glide无法加载此类型的url图像



我以前使用过Glide很多次,但目前我在使用这种类型的urlhttps://www.publicdomainpictures.net/pictures/10000/nahled/thinking-monkey-11282237747K8xB.jpg显示图像时遇到了问题,我不知道为什么我无法加载它。我甚至简化了我的项目,只在MainActivity上显示此图像。当我检查glide的监听器时,它会提示这个错误

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   

问题是,我试过其他的Url,它们都很好用。可能出了什么问题?这里是我的简单代码

主要活动.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)

某些东西可能配置不正确。看看这些。

  • 首先,您不需要tools:srcCompat="@tools:sample/avatars"

  • 第二,把这个放在AndroidManifest.xml文件的标签里。

android:usesCleartextTraffic="true"
  • 你应该知道glid中图片的大小规则,因为在滑翔中加载大图像时存在问题

  • 您也可以尝试将android:largeHeap="true"放入清单文件的标记中。

让我知道结果:)

更新

如果它还不起作用,试试这个。


Glide.with(getContext())
.asBitMap() //[for new glide versions]
.load(url)
//.asBitmap()[for older glide versions]
//.placeholder(R.drawable.default_placeholder)
.override(1600, 1600) // Can be 2000, 2000
.into(new BitmapImageViewTarget(imageViewPreview) {
@Override
public void onResourceReady(Bitmap  drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);   
}
});

或者这个


Glide.with('context')
.asBitmap()
.load('url')
.apply(new RequestOptions().override(1600, 1600)) //This is important 
.into(new BitmapImageViewTarget('imageview') {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
imageview.setImageBitmap(resource);        
}
});

正在清单中添加网络安全配置xml文件:

EN:将网络安全配置xml文件添加到列表中:res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">developer.alexanderklimov.ru/</domain>
</domain-config>
</network-security-config>

AndroidManifest.xml中添加网络安全配置:

EN:在AndroidManifest.xml:中添加网络安全配置

<application
android:name=".MyApplication"
android:networkSecurityConfig="@xml/network_security_config" ... />

[发件人]https://www.5axxw.com/questions/content/uhfo5y

最新更新