如何处理Glide V4中找不到图片的错误

时间:2019-07-02 05:35:17

标签: android kotlin android-glide

我尝试处理Glide中未找到(404)的图片错误,但无法编译:

Glide.with(this@ImageActivity)
                        .addDefaultRequestListener(
                                object:RequestListener<Drawable> {
                                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                                    }

                                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                                    }

                                }

                        )
                        .load(imageURL).apply(requestOptions)
                        .into(imageview)

但是编译器会在 object:RequestListener 上引发此错误:

  

类型不匹配:推断的类型为,但   RequestListener!是预期的

3 个答案:

答案 0 :(得分:1)

您必须像这样使用Submit()-

df['Temp']

答案 1 :(得分:1)

如果要使用addDefaultRequestListener,则需要使用Any而不是Drawable

Glide.with(context).addDefaultRequestListener(object : RequestListener<Any> {
            override fun onLoadFailed(e: GlideException?, model: Any?, target: com.bumptech.glide.request.target.Target<Any>?, isFirstResource: Boolean): Boolean {
            }

            override fun onResourceReady(resource: Any?, model: Any?, target: com.bumptech.glide.request.target.Target<Any>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            }

        })

答案 2 :(得分:0)

您可以仅通过placeholder()方法使用Glide。您可以使用以下方法。

private fun fetchImage(imageView: ImageView, url: String){
    Glide
        .with(this)
        .load(url)
        .centerCrop()
        .placeholder(R.drawable.ic_menu_camera)
        .into(imageView)
}