62

I have a pretty basic load image from server line code:

Glide.with(view.getContext()).load(url).placeholder(R.drawable.default_profile).into(view);

For some reason, I'm always stuck with the placeholder being displayed and never the real image!

I have made sure that a valid and working url is being passed. And, if I use the same code without the placeholder it works fine

Glide.with(view.getContext()).load(url).into(view);

Any ideas why?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
TareK Khoury
  • 12,721
  • 16
  • 55
  • 78
  • 4
    For security reason, Android doesn't allow some of URL's. Think to add `android:usesCleartextTraffic="true"` in your manifest file. Detailed answer is [here](https://stackoverflow.com/a/50834600/1744705) – TeachMeJava Jul 21 '19 at 14:29

15 Answers15

104

Try to add .dontAnimate() It caused by TransitionDrawable too and it seems so because after scroll there's no animation because it's cached.

The correct code is

Glide.with(view.getContext()).load(url).placeholder(R.drawable.default_profile).dontAnimate().into(view);

I hope it will be helpful for you.

Anbuselvan Rocky
  • 606
  • 6
  • 22
Abbes Yassine
  • 1,679
  • 2
  • 17
  • 17
  • 3
    For some weird reason, this solved the issue. The transition is ugly though without the animation. – TareK Khoury Apr 04 '16 at 06:26
  • 1
    Place holder is not exist in Glide v4.0.0 – Mahdi Aug 07 '17 at 10:26
  • Just as Glide's README.md says, CircleImageView/CircularImageView/RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue. – Veer Sep 29 '17 at 07:09
59

Check if you have added Internet permission in the manifest:

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

Glide does not fire exception if there is no Internet connectivity.

Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
  • 7
    Indeed ! This solved the trouble for me. I think it would be more useful if glide raises an Exception when developper makes such a mistake. – Thilaw Fabrice Mar 09 '17 at 09:16
10

Add android:usesCleartextTraffic="true" in the application tag in manifest and check the internet permission is mentioned or not in the same manifest file:-

4

Add this permission below to access resource endpoint/url

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

If your target endpoint/url only has http add this code below in your manifest.xml. Starting with Android 9 (API level 28), cleartext support is disabled by default.

android:usesCleartextTraffic="true"

Because of that, if you get resource from your unencrypted HTTP API don't forget to add

res/xml/network_security_config.xml

So the code will be like these

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">urweb.id</domain>
        <domain includeSubdomains="true">localhost</domain>
        ...
        <domain includeSubdomains="true">111.222.333.444</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>

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

    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
    
        <activity>
        ...
        </activity>
    </application>

</manifest>
Aldy
  • 2,743
  • 2
  • 12
  • 17
3

If you are tried following steps :

  • INTERNET permission is already added both in Manifest and Programmatically
  • usesCleartextTraffic : if already mentioned in Manifest Even though image is not loading in ImageView

Then try this :

  • add this line application tag of Manifest
android:usesCleartextTraffic="true"
  • add following code in the activity or fragment at top in which you are trying to perform the operation
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Rahul Kamble
  • 202
  • 2
  • 3
2

Use ProgressBar as loading gif

Glide.with(context).
        load(url)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .crossFade(1000)
        .into(imageView);
庄景鹏
  • 960
  • 9
  • 7
2

If someone comes across this in the future you may need to add

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

Unsure of the exact reason for this change, nonetheless my code didn't work without that permission, and now does with it.

2

Below is a weird fix that worked for me.

The following always fails for me (12 trials) - I never see the real image:

Glide.with(context)
 .load(url)
 .asBitmap()
 .into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) { ... }

The following always succeeds for me (12 trials) - I always see the real image:

Glide.with(context)
 .load(url)
 .asBitmap()
 .listener(new RequestListener() {
    public boolean onException(Exception e,Object o,Target t,boolean b)
               {return false;}
    public boolean onResourceReady(Object o,Object p,Target t,boolean b,boolean c)  
               {return false;}})
 .into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) { ... }

As you can see, the only difference here is that I added a listener. Makes little sense, but these trials were done pretty carefully, so I'm going with it.

Jo Jo
  • 7,245
  • 5
  • 34
  • 49
2

None of the solutions above worked for me. The issue may be at the placeholder you're using. If the view you're using to load the image has a placeholder, it causes some issues. Removing that placeholder for some reason seems to fix it.

So, if the (Image)View you're trying to inflate has a placeholder, such as android:src="@mipmap/placeholder", remove it.

        <ImageView
            android:id="@+id/imgGallery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@mipmap/rugova1" />

like this

 <ImageView
            android:id="@+id/imgGallery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            />

It worked for me.

Bleron Qorri
  • 33
  • 1
  • 4
1

Seems strange but only thing I could guess is Your URL is valid as You already said. Your remote is getting downloaded even getting applied on your image view but your placeholder is somehow hiding it. Glide has some bugs related to placeholder stuff.

My suggestion would be to try below:

Glide.with(view.getContext()).load(url).
placeholder(R.drawable.default_profile).fitCenter().into(view);

So the trick is placeholder is set via setImageDrawable() so the ImageView will just display it as usual, but you tell Glide to use the fitCenter explicitly which will fit the loaded image nicely within the ImageView's laid out size via a Transformation and then set it via setImageDrawable(). Since the fitted image is a perfect fit, center will just draw the image covering the whole area of the view.

Give it a try.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
theJango
  • 1,100
  • 10
  • 22
1

Dont use transition or animation for Glide.It will solve your problem

Code :

Glide.with(context)
    .load(horizontalHappyHourList.get(position).getImage())
//  .transition(DrawableTransitionOptions.withCrossFade(400)) //Optional
    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE)
    .error(R.drawable.no_image))
    .into(holder.imageView);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

May help someone :) My issue was network security policy exception. The URL requested by Glide was blocked by android due to security policy.

Fixed by whitelisting desired domain. Check here

com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.net.UnknownServiceException(CLEARTEXT communication to maps.googleapis.com not permitted by the network security policy)
 call GlideException#logRootCauses(String) for more detail

The toughest thing is it was not shown in the normal log.

Ebin Joy
  • 2,690
  • 5
  • 26
  • 39
0

Incase none of the above solves it, and you're setting the 'src' property on the ImageView, it won't work. I was mistakenly setting the 'src' on the ImageView in xml and taking that out solved it for me.

0

In my case, I had a problem with the layout_height attr that I set in xml

<ImageView
        android:id="@+id/image_view_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/linear_layout_content"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintLeft_toLeftOf="@id/linear_layout_content"
        app:layout_constraintRight_toRightOf="@id/linear_layout_content"
        app:layout_constraintTop_toTopOf="@id/linear_layout_content" />

Glide was giving a line of warning in the logcat which I don't remember what it was but it was suggesting to avoid using wrap_content for width and height.

So all I did was modify the layout to avoid using wrap_content and the problem was solved.

I tried to regenerate that problem to get the warning line after the problem was solved but I couldn't. It seems to be a cache problem in the library.

0

Make sure to check the url (link) as it may be a page link not an exact image address, try copying the link and paste it to the browser to see if it opens just image or a web page and if it opens a web page then no doubt that is the problem it is a page link and again not an image address.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31656516) – A S M Sayem May 04 '22 at 19:16