我正在尝试从url异步加载图片(在后台线程上)。
尝试这样的事情:
val futureTarget = glide
.asBitmap()
.load("https://dummyimage.com/300/09f/fff.png")
.submit(300, 300)
val bitmap = futureTarget.get()
但是,当尝试从URL加载任何图像时,它会使应用程序崩溃,并且在加载本地资源时可以正常工作。
Logcat
There was 1 cause:
com.bumptech.glide.load.HttpException(Not Found)
call GlideException#logRootCauses(String) for more detail
at com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:205)
at com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:108)
at com.....Notifier$sendNotificationTest$1.run(Notifier.kt:85)
at java.lang.Thread.run(Thread.java:764)
Caused by: com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
com.bumptech.glide.load.HttpException(Not Found)
call GlideException#logRootCauses(String) for more detail
我添加了Internet权限。
<uses-permission android:name="android.permission.INTERNET" />
使用Dagger 2提供Glide
@Provides
@Singleton
static RequestManager provideGlide(final Application application) {
return GlideApp.with(application);
}
知道我缺少什么吗?
更新
我发现某些HttpInterceptor存在问题。该解决方案可以同步下载位图。
答案 0 :(得分:0)
尝试一下。
val options = RequestOptions()
.placeholder(R.drawable.your_placeholder_image)
.error(R.drawable.your_error_image)
Glide.with(this).load(image_url).apply(options).into(imageView)
编辑:-获取Callbacks
Glide.with(this)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO: something on exception
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
Log.d(TAG, "OnResourceReady")
//do something when picture already loaded
return false
}
})
.into(imgView)
答案 1 :(得分:0)
尝试一下...。这将为您提供帮助。
Glide.with(this)
.load(image_url)
.into(imageView)
.placeholder(R.drawable.your_placeholder_image)
.error(R.drawable.your_error_image)