1

I'm trying to load an image from url async (On a background thread).

Trying something like this:

  val futureTarget = glide
    .asBitmap()
    .load("https://dummyimage.com/300/09f/fff.png")
    .submit(300, 300)

  val bitmap = futureTarget.get() 

But it crashes the app when trying to load any image from URL, it works fine when loading local resources.

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

I have added Internet permissions.

<uses-permission android:name="android.permission.INTERNET" />

Providing Glide using Dagger 2

@Provides
@Singleton
static RequestManager provideGlide(final Application application) {
    return GlideApp.with(application);
}

Any idea what I'm missing?

UPDATE

I figured out that there was an issue with some HttpInterceptors. This solution works downloading bitmaps synchronously.

ArbenMaloku
  • 548
  • 4
  • 15

2 Answers2

1

try this..

    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)

EDIT:- To get 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)
Prachi Singh
  • 564
  • 3
  • 8
-2

try this.... this will help you.

Glide.with(this)
  .load(image_url)
  .into(imageView)
  .placeholder(R.drawable.your_placeholder_image)
  .error(R.drawable.your_error_image)
Basi
  • 3,009
  • 23
  • 28