【问题标题】:Glide Android - Failed to load resource from urlGlide Android - 无法从 url 加载资源
【发布时间】:2019-06-11 09:04:54
【问题描述】:

我正在尝试从 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 存在问题。此解决方案可同步下载位图。

【问题讨论】:

  • 您是否在清单中启用了互联网权限?
  • 请检查您是否在AndroidManifest.xml中添加了INTERNET_PERMISSION。
  • 是的,我在尝试加载图像时在应用程序内。
  • 你在创建 glide 的实例吗?
  • 是的,刚刚编辑了问题:)

标签: android kotlin android-glide android-image


【解决方案1】:

试试这个..

    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)

【讨论】:

  • 思路是同步下载图片。我不想使用回调。
  • @ArbenMaloku 你可以添加监听器来获取回调
【解决方案2】:

试试这个....这会对你有所帮助。

Glide.with(this)
  .load(image_url)
  .into(imageView)
  .placeholder(R.drawable.your_placeholder_image)
  .error(R.drawable.your_error_image)

【讨论】:

  • 请在您的帖子中解释为什么这会有所帮助。欢迎任何解释。
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2020-05-04
  • 2015-07-11
  • 1970-01-01
  • 2023-02-22
相关资源
最近更新 更多