43

I am trying to load an image into imageView through Glide. But the image is not loaded - I get an error. I'm using the following code

GlideApp.with(context)
    .load(itemData.getThumbnailUri())
    .placeholder(R.mipmap.koya_logo_white)
    .error(R.mipmap.ic_image_loading_error)
    .into(itemBinding.cover);

Logs

lide: Load failed for https://s3.amazonaws.com/koya-dev-videos/kindness/8da807aa-1e1e-413d-bf9b-5bb084646593/medialibrary/9456621508/videos/1eb78337-d569-41bd-95ad-153d9098de03.png with size [1080x1080]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{StringUri->Object->Drawable}, LOCAL, DataCacheKey{sourceKey=https://s3.amazonaws.com/koya-dev-videos/kindness/8da807aa-1e1e-413d-bf9b-5bb084646593/medialibrary/9456621508/videos/1eb78337-d569-41bd-95ad-153d9098de03.png, signature=EmptySignature}
Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Drawable->Drawable}
Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Bitmap->Drawable}
Zoe
  • 27,060
  • 21
  • 118
  • 148
narender sharma
  • 561
  • 1
  • 4
  • 7

18 Answers18

7

Another reason why this problem appears is the phone/emulator doesn't have an internet connection.

Ievgen
  • 426
  • 6
  • 10
6

Try this solution...

String url = "https://s3.amazonaws.com/koya-dev-videos/kindness/8da807aa-1e1e-413d-bf9b-5bb084646593/medialibrary/9456621508/videos/1eb78337-d569-41bd-95ad-153d9098de03.png";
    
GlideApp.with(context).load(url)
        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
        .error(R.drawable.glide_app_img_loader)
        .listener(new RequestListener<Drawable>() {
             @Override
             public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                 return false;
             }
    
             @Override
             public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                 return false;
             }
        }).into(imageView);
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49
  • class com.bumptech.glide.load.engine.GlideException: Failed to load resource There were 2 causes: com.bumptech.glide.load.HttpException(Forbidden) com.bumptech.glide.load.HttpException(Forbidden) call GlideException#logRootCauses(String) for more detail Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE – narender sharma Apr 02 '19 at 11:39
  • 1
    are you sure you are using only HTTPS urls ? – VIISHRUT MAVANII Apr 04 '19 at 06:04
  • 1
    @VishrutMavani thanks for the great tip! I had no idea you could do this. – Fattie Apr 05 '21 at 18:40
  • 1
    I'm not using url, but internal storage and the override with target sizes did the trick! Thanks! – Hector Costa Apr 08 '21 at 14:22
5

The solution works for me:
1. Update build.gradle(Module: app)

    implementation "com.github.bumptech.glide:glide:4.7.1"
        kapt "com.github.bumptech.glide:compiler:4.7.1"
        implementation "com.squareup.okhttp3:okhttp:3.14.0"
        implementation ('com.github.bumptech.glide:okhttp3-integration:4.7.1'){
            exclude group: 'glide-parent'
        }
  1. Set timeout for glide

     @GlideModule
        class MyAppGlideModule : AppGlideModule() {
            override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
                val client = OkHttpClient.Builder()
                    .readTimeout(30, TimeUnit.SECONDS)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .build()
                val factory = OkHttpUrlLoader.Factory(client)
                glide.registry.replace(GlideUrl::class.java, InputStream::class.java, factory)
            }
        }
    
Anh Duy
  • 1,145
  • 15
  • 25
5

Simplest solution

add,

android:usesCleartextTraffic="true"

inside your <application/> tag of AndroidManifest.xml file.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
  • 1
    @IlyaGazman exactly, The default value target API level 27 or lower is "true". Apps that target API level 28 or higher default to "false" – Kishan Solanki Mar 15 '21 at 04:32
  • it worked for me! you don't need this when using api level 8 downwards but when working on api 8.1 and above you will need this particular solution – Theophilus Abiola Alamu Mar 18 '21 at 17:07
2

Update the Glide version to :

 implementation 'com.github.bumptech.glide:glide:4.14.2'
 annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'
Kumar
  • 442
  • 1
  • 9
  • 19
1

Am also faced this issue.Its bug from glide side.Use the latest version of glide.

repositories {
 mavenCentral()
 google()
}

dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

Make sure that itemData.getThumbnailUri() not contain empty space

sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

I also had this problem. When I wrote the layout like this

<FrameLayout 
    android:layout_width="@dimen/view_width"
    android:layout_height="@dimen/view_height">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>

and I changed that to

<FrameLayout 
    android:layout_width="@dimen/view_width"
    android:layout_height="@dimen/view_height">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

This solution worked for me.

0

Same with @ievgen answers.

  • Check for your internet connection
  • HTTPS connection issue for Android 8 or 9, see here
  • The issue might occur due to invalid URL or having a trailing space or / slash

My project was getting an API from TMDB, and using val BASE_IMAGE = http://image.tmdb.org/t/p/w780/ notice the last trailing w780/ "slash"?

It is concatenated mistakenly e.g:

W/Glide: Load failed for http://image.tmdb.org/t/p/w780//qJdfO3ahgAMf2rcmhoqngjBBZW1.jpg with size [-2147483648x-2147483648]

Removing the trailing / slash fixed my issue. E.g from:

http://image.tmdb.org/t/p/w780/ -> http://image.tmdb.org/t/p/w780

Here's my setup:

Glide: 4.9.0
Android Studio: 3.4.1 (Grale Build Tools)
mochadwi
  • 1,190
  • 9
  • 32
  • 87
0

Create image decoder like this:

class PageDecoder(
    private val bitmapPool: BitmapPool
) : ResourceDecoder<InputStream, Bitmap> {

    companion object {
        val PAGE_DECODER: Option<Boolean> = Option.memory("abc")
    }

    override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<Bitmap>? {
        return BitmapResource.obtain(BitmapFactory.decodeStream(source), bitmapPool)
    }

    override fun handles(source: InputStream, options: Options): Boolean = options.get(PAGE_DECODER) ?: false

}

In your glide module should apply this:

override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.prepend(InputStream::class.java, Bitmap::class.java, PageDecoder(glide.bitmapPool))
    }

In activity there you load image use this:

GlideApp.with(context)
        .load("Image url or path, etc")
        .apply(GlideOptions.option(PageDecoder.PAGE_DECODER, true))
        .into(imageview)

It's work for me, I hope it's was helpfull for you.

0

for me the url was " https//app.domain.." the issue here is the space at the beginning of the url

Mostafa Onaizan
  • 923
  • 1
  • 5
  • 17
0
Glide.with(this)
   .load(imageItem.img_url.toUri().buildUpon().scheme("https").build())
   .into(mars_image)

Try to extend your url with load(imageItem.img_url.toUri().buildUpon().scheme("https").build()) this extension for https.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
0

I had the same issue and this is what worked for me. Replacing the http to https.

   if (url.startsWith("https")) {
        Glide.with(imageView.getContext())
                .load(url)
                .error(R.drawable.thumblin)
                .fitCenter()
                .into(imageView);
    } else {
        Glide.with(imageView.getContext())
                .load(url.replace("http","https"))
                .error(R.drawable.thumblin)
                .fitCenter()
                .into(imageView);
    }
Manisha Saini
  • 51
  • 1
  • 7
-1

Check if the Uri you are passing is correct ,for me it was bug in the Uri being passed as I was Appending the required BaseUri twice

-1

This worked for me in Kotlin

 Glide.with(context).load(url)

                    .override(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).listener(
                                object : RequestListener<Drawable> {
                                    override fun onLoadFailed(
                                        e: GlideException?,
                                        model: Any?,
                                        target: Target<Drawable>?,
                                        isFirstResource: Boolean
                                    ): Boolean {
                                        return false
                                    }

                                    override fun onResourceReady(
                                        resource: Drawable?,
                                        model: Any?,
                                        target: Target<Drawable>?,
                                        dataSource: DataSource?,
                                        isFirstResource: Boolean
                                    ): Boolean {
                                        return false
                                    }

                                }
                            ).into(imageView)
  • 2
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 10 '21 at 10:05
-1

I was having same problem with Glide + AndroidSVG as some Vector images were not loaded and having to check between SVG and PNG for Glide to process correctly.

here is the alternate solution presented by @sadegh . Please go ahead to direct answer.

ALTERNATIVE WAY : kotlin + Coil

This solution will work well with .svg , .png , .jpg

add dependency:

//Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:1.4.0")
implementation("io.coil-kt:coil-svg:1.4.0")

add this function to your code:

fun ImageView.loadUrl(url: String) {

val imageLoader = ImageLoader.Builder(this.context)
    .componentRegistry { add(SvgDecoder(this@loadUrl.context)) }
    .build()

val request = ImageRequest.Builder(this.context)
    .crossfade(true)
    .crossfade(500)
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .data(url)
    .target(this)
    .build()

imageLoader.enqueue(request)
}

Then call this method in your activity or fragment:

  imageView.loadUrl(url)
  // url example : https://upload.wikimedia.org/wikipedia/commons/3/36/Red_jungle_fowl_white_background.png
Htet Will
  • 21
  • 1
  • 5
-2

Try to uninstall your app from emulator and try compile again. It's working for me.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 15 '22 at 07:23
-5

As we use the AWS and with my experience to it found that Glide is not able to load HTTP URLs but works fine if we use HTTPS over the HTTP.

Also, it fails to loads large images for say resolution 1800x1800 or more.

Why? It fails to load that much data of an image on some specific device due to less heap. So the best option is to use RequestOptions and apply it to the Glide.

try {
    String url = "" /* URL of Image */;

    if (url.startsWith("http://"))
        url = url.replace("http://", "https://");

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.mipmap.app_icon);
    requestOptions.error(R.mipmap.app_icon);
    Glide
        .with(context)
        .setDefaultRequestOptions(requestOptions)
        .load(url)
        .into(imgView);
} catch (Exception e) {
    e.printStackTrace();
}
Harpreet
  • 2,990
  • 3
  • 38
  • 52
-7

I think you did not add permission for your app to access the Internet.

Add this into your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
Phạm Thanh
  • 1
  • 1
  • 2