Android Kotlin - Glide 侦听器将图像作为位图下载到变量并使占位符出错

Android Kotlin - Glide listener download image as Bitmap to variable and make placeholder onerror

我需要下载一张图片并将其保存为变量以将其放入通知中:

.setLargeIcon(bitmap)

我希望这是代码,我尝试的评论可以清楚地表明:

        var bitmap = BitmapFactory.decodeResource(this@MainActivity.resources, R.drawable.notif_smiley) // create placeholder bitmap

        val requestOptions = RequestOptions()
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.NONE)

        bitmap = Glide.with(this@MainActivity)
            .asBitmap()
            .load(imgurl)
            .listener(object : RequestListener<Bitmap> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    // just dont do anything, keep the placeholder bitmap
                    return false
                }
            })
            .apply(requestOptions)
            .submit()
            .get()


        binding.contentMain.testingGlide.setImageBitmap(bitmap) // this is just for easy testing

我遇到了各种各样的错误,我根据一个答案尝试了这个,但是位图直接进入了视图,请帮忙 :D

在加载失败的方法中,您没有设置任何图像,因此请尝试更改它,同时尝试在代码实现下方添加 onerror() 方法应该修复它

try{
     lateinit var bitmap:Bitmap
           bitmap =  Glide.with(context)
                    .asBitmap()
                    .placeholder(R.mipmap.avengerswallp)
                    .load(imgUri)
                    .error(getnothumb())
                    .listener(object : RequestListener<Bitmap>{
                        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                            bitmap = myPlaceHolderBitmap
                            return true
                        }

                        override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                            return false
                        }
                    }).submit().get()
}
catch(ex:Exception)
{
}