Glide not loading real image and stuck with placeholder - android
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?
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.
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.
Add android:usesCleartextTraffic="true" in the application tag in manifest and check the internet permission is mentioned or not in the same manifest file:-
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>
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);
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.
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.
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.
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);
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.
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);
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.
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.
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.
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.
Just uninstall app and reinstall it. It will work
Related
Android WebView and network-security-configuration
I'm developing on Android 8 (26 API, Oreo) and I use android.webkit.WebView in my app. I would implement "secure network connection" when I load pages with my WebView (in other words I would avoid man-in-the-middle problems and self-signed certificates) To do this I used network security configuration (on Android from version 7.0 N, 24 API) So: In res>xml>network_security_config.xml <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config> <domain includeSubdomains="true">MY_DOMAIN.com</domain> <pin-set> <pin digest="SHA-256">MY_PIN</pin> </pin-set> </domain-config> </network-security-config> I found MY_PIN inserting MY_DOMAIN.com here: https://report-uri.com/home/pkp_hash In manifest>AndoridManifest.xml ... <application android:networkSecurityConfig="#xml/network_security_config" ... </application> In the onCreate of my app I simply do: WebView webView = new WebView(this); webView.setWebViewClient(new WebViewClient() { #Override public void onReceivedSslError(..).. #Override public void onPageFinished().. ...}); webView.loadUrl(MY_DOMAIN.com); According to Android docs I'm doing it right but I have a problem: it's like network_security_config.xml is never checked because I can set every "random" and "wrong" value for the pin and it works normally (URL MY_DOMAIN.com is loaded normally without blocking behavior). So that means that if some man-in-the-middle return back one different pin of those I've written in res>xml>network_security_config.xml the application continue running well and with no secure behavior. It also does not execute one of the overridden error method of WebViewClient. Please help I can not understand my error.
[SOLVED] In AndoridManifest.xml I declared <application android:networkSecurityConfig="#xml/network_security_config" ... </application> Editor warned about a problem related to the SDK version but I didn't see it. This is the warning. [SOLUTION] Add this tools:targetApi="n" to the Manifest like the following: <application android:networkSecurityConfig="#xml/network_security_config" ... tools:targetApi="n"> [EDIT] SSL error is handled in public void onReceivedSslError(...) of WebViewClient (See the following code) webView.setWebViewClient(new WebViewClient() { public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) { //HANDLE HERE THE ERROR!!! ... } ... });
Android DataBinding - Why Data Binding not support #mipmap images
Using DataBinding, I trying to access icon from mipmap folder, after write a code it not compile model class and it show error in log - "token recognition error". Look at the below screenshot even it not allowed #mipmap. If anyone know the reason Please let me know <ImageView android:id="#+id/img_page1" android:layout_width="#dimen/splash_slider_circle" android:layout_height="#dimen/splash_slider_circle" android:src="#{slider.img1 ? #mipmap/ic_circle_filled : #mipmap/ic_circle_outline}" />
The exact reason for the described behaviour is unknown to me. But if it's inconvenient to move all your icons from mipmap to drawable folders, it's possible to import your generated R class in the layout and refer mipmaps in conditions starting with R.mipmap and using app:imageResource attribute: <data> <import type="your.package.R" /> ... </data> <ImageView ... app:imageResource="#{someConditionVariable ? R.mipmap.ic_for_true_condition : R.mipmap.ic_for_false_condition}" ... />
Is there a way to add a TAG to a NetworkImageView with Volley
It is very easy to add a TAG to a request with Volley, but when it comes to a NetworkImageView, I don't see a way to specify a TAG to the request that is handled and added to the queue by the ImageLoader. A request TAG would help me to easily identify the request and react consequently. Therefore, how can I add a TAG to a request made by a NetworkImageView ?
There is currently no way unless you override ImageLoader and NetworkImageView. The 'guy' who is creating the request is ImageLoader.makeImageRequest so you need to pass the tag to it. However you can use jus which is based on volley and support this feature. you currently need to use the SNAPSHOT version: compile 'io.apptik.comm:jus-android:0.7.0-SNAPSHOT' and you can set the tag using the "requestTag" argument in the xml layout: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragments.JusFragment"> .... <io.apptik.comm.jus.ui.NetworkImageView android:id="#+id/networkImageView" android:layout_width="150dp" android:layout_height="170dp" app:requestTag="ImageTag1" /> .... OR from the code before you call setImageUrl(): networkImageView.setRequestTag("ImageTag2"); networkImageView.setImageUrl(url, imageLoader);
Support library VectorDrawable Resources$NotFoundException
I am using Design Support Library version 23.4.0. I have enabled the gradle flag: defaultConfig { vectorDrawables.useSupportLibrary = true } I am using build tools version 23.0.2, but still, I am getting Resources$NotFoundException on KitKat or lower. It is occurring when I use android:drawableLeft or imageView.setImageResource(R.drawable.drawable_image). And yes, I am putting this on every activity where I am using drawables static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } Is this a bug of the support library?
It took 3 separate things for me to get this to work using support library 23.4.0: Add this to build.gradle defaultConfig { vectorDrawables.useSupportLibrary = true } Add the following to onCreate of your Application class AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); (From the reference of this link - "https://stackoverflow.com/a/45582033/10752962") In API less then 21,use this line before setContentView(); For all XML views in which you are setting a vector drawable replace android:src with app:srcCompat and in the code replace this: imageView.setImageResource(...); with imageView.setImageDrawable(...);
To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases. In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).
Try using: imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null)); You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this way. Just inflate your vector drawables using VectorDrawableCompat and you're all set.
We had the same issue. Vector drawables were not visible on Kitkat. I solved this issue by adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); to the onCreate method of Activities. Before that dont forget to add: defaultConfig { vectorDrawables.useSupportLibrary = true } and call setImageResource for the view that you use the vector drawable. My view is ImageButton. I have Android SDK build tools version 23.0.3
Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities. 1. Create a class which extends to Application (android.app.Application) public class MyApplicationClass extends Application { #Override public void onCreate() { super.onCreate(); } } 2. Head over to Manifest.xml and add the following line to your tag <application android:name=".MyApplicationClass" android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="#style/AppTheme"> ... </application> 3. Add the following code above onCreate in MyApplicationClass.java // This flag should be set to true to enable VectorDrawable support for API < 21 static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } Complete code for MyApplicationClass.java import android.app.Application; import android.support.v7.app.AppCompatDelegate; /** * Created by Gaurav Lonkar on 23-Dec-17. */ public class MyApplicationClass extends Application { // This flag should be set to true to enable VectorDrawable support for API < 21 static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } #Override public void onCreate() { super.onCreate(); } }
defaultConfig { vectorDrawables.useSupportLibrary = true } use this in app.gradle Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me
Support for vector drawables in places like android:drawableLeft was disabled in support library 23.3. It was announced on Google+: we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource() continues to work. Links to issues: https://code.google.com/p/android/issues/detail?id=205236 https://code.google.com/p/android/issues/detail?id=204708 However, if you can live with those issues, in 23.4 you can re-enable this functionality using AppCompatDelegate.setCompatVectorFromResourcesEnabled(). If you're curious how this works, the best person to learn from is Chris Banes, who authored this functionality. He explains in detail on his blog.
change imageView.setImageResource(R.drawable.drawable_image) to imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image)); if you want to use vectordrawable in xml, use this: app:srcCompat="#drawable/drawable_image"
I had a similar problem long ago, it did not work by setting vectorDrawables.useSupportLibrary = true only worked when I created the "mipmap" folder, and the code used imageView.setImageResource (R.mipmap.drawable_image) It has more Info here
Inflating Drawable's `VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way: Calling static getDrawable() methods: //This will only inflate a drawable with <vector> as the root element VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector); //This will only inflate a drawable with <animated-vector> as the root element AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector); // This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices ResourcesCompat.getDrawable(context, R.drawable.any_drawable); If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient. The library has the following morph (bi-directional) animations : Play-Pause morph animation Play-Stop morph animation Arrow-Hamburger menu morph animation As you can see, I produced the above image on my API 16 phone: import com.wnafee.vector.compat.AnimatedVectorDrawable; mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector); Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file): dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:design:25.0.0' //not needed // compile 'com.android.support:support-vector-drawable:25.0.0' compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat // Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0 //not needed // compile 'com.android.support:support-animated-vector-drawable:25.0.0' }
Do not put your vectors in drawable-anydpi , old devices does not support that put them in drawable
In my particular case, I had this problem because I was using a drawable selector as the image resource with several vectors in the selector, as in: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="#drawable/vector_select_blue"/> <item android:state_pressed="true" android:drawable="#drawable/vector_select_black"/> . . etc </selector> Yes, pretty bad, but didn't know better at the time. So, the right way of doing this is using the tint property in your vector file, as in: <vector ..vector properties.. android:tint="#color/vector_color_selector"> <path ..path properties../> </vector> (You can also use the app:tint attribute in the AppCompatImageView) And now, your vector_color_selector file should have the colors you want, as in: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="#color/blue"/> <item android:state_pressed="true" android:color="#color/black"/> . . etc </selector> I hope this helps someone if previous answers didn't work for you. Stating the obvious, but I must say that you still need to set vectorDrawables.useSupportLibrary = true in gradle, use AppCompatImageView and use app:srcCompat or setImageDrawable + AppCompatResources.getDrawable to avoid any troubles with the vector compat library.
Use AppCompatImageView instead of ImageView as said by Harish Gyanani in comments , it works fine with this for me. Official docs
I had the same problem and actually what was missing is I was using app:srcCompat on AppCompatTextView except of AppCompatImageView. The way I have found the problematic part: My error looks like: Fatal Exception: android.content.res.Resources$NotFoundException Resource ID #0x7f0700d1 Here are the steps I followed the resource id of the mentioned drawable : APK Analyzer -> classesXXX.dex In this dex file I opened the directory of my apps package name and went to R$drawable file R$drawable -> Show as byte code. Search for ID [0x7f0700d1] (check your own ID) Find the image and check for all the usages (CMD + F7) of the resource Fix Hope it will help somebody.
Picasso Main Errored
I am trying to use Picasso Android library but i'm not able to get it working. For starting, i am trying the simplest of the things: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_view); mImageView = (ImageView) findViewById(R.id.imageView); Picasso.with(this).setIndicatorsEnabled(true); Picasso.with(this).setLoggingEnabled(true); Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(mImageView); } the activity layout is: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="#+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF0000"/> </RelativeLayout> The view remains completely red. If i set a resource in the view via setImageResource, it correctly displays. The only relevant clue that something went wrong is a message saying: Picasso: main errored but i can't understand why. I would happily paste more logcat output but it breaks the code block and becomes unreadable. Here's a pastebin: main errored log I believe there's something trivial i am doing wrong.
To download images from the internet, you will need the INTERNET permission in your manifest. <uses-permission android:name="android.permission.INTERNET" />