'Glide not loading real image and stuck with placeholder
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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
Add android:usesCleartextTraffic="true" in the application tag in manifest and check the internet permission is mentioned or not in the same manifest file:-
Solution 4:[4]
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);
Solution 5:[5]
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.
Solution 6:[6]
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.
Solution 7:[7]
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);
Solution 8:[8]
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.
Solution 9:[9]
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);
Solution 10:[10]
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>
Solution 11:[11]
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.
Solution 12:[12]
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.
Solution 13:[13]
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.
Solution 14:[14]
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.
Solution 15:[15]
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.
Solution 16:[16]
Just uninstall app and reinstall it. It will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow