Android Glide doesn't get the newest version of the picture - android

Trying to work with Glide for Android and Firebase.
My code is working perfectly (uploading, downloading into ImageViews), my problem is that when I change the image in the Firebase Storage (for example, a user image), the app doesn't "know" that the image in the database has changed and keeps showing the cached version.
Obviously I want the app to use cache, but in a way that knows if the original image has changed in the database.
Is there a way to do it?

if cache is not wanted, you can try
Glide.with(ctx)
.load(Uri))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImage);
And also This will remove cache memory which is stored by Glide.
Glide.get(ctx).clearDiskCache();

Related

How to Cache Images in Android coming from server and show it?

I am developing an Android application where I need to show some images in a recyclerview. I want to download the images at once and probably save it in cache or internal storage of the android and then show the images from the internal storage if there are images in storage and if not then load and download from server.
And I don't want these images to appear in the user's Gallery App.
I just want to know the architecture , not the code about how to do it.
Is there any difference between the cache memory and internal storage? And do I need the permission to save the images in cache?
EDIT:
My question is different from the one suggested by #Dima, in that question he is not caching the images or save it in internal storage, i reckon.
Glide is a great library for showing image asynchronously.
Glide's disk cache strategies:
Glide 3.x & 4.x: DiskCacheStrategy.NONE caches nothing, as discussed
Glide 4.x: DiskCacheStrategy.DATA, Glide 3.x: DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one
Glide 4.x: DiskCacheStrategy.RESOURCE Glide 3.x: DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior of Glide 3.x)
Glide 4.x only: DiskCacheStrategy.AUTOMATIC intelligently chooses a cache strategy based on the resource (default behavior of Glide 4.x)
Glide 3.x & 4.x: DiskCacheStrategy.ALL caches all versions of the image
As a last example, if you've an image which you know you'll manipulate often and make a bunch of different versions of it, it makes sense to only cache the original resolution. Thus, we'd tell Glide to only keep the original:
example:
Glide 4.x
GlideApp
.with(context)
.load(eatFoodyImages[2])
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(imageView3);
Glide 3.x
Glide
.with( context )
.load( eatFoodyImages[2] )
.diskCacheStrategy( DiskCacheStrategy.SOURCE )
.into( imageViewFile );
and the cached will be in data folder of your app so won't show up in user gallery.

Loading different images from cache using Glide Library

i am retrieving images URL using volley from MySQL Database , when am online i have no problem , but i need to make it offline too so am using SQLite to store data, but when the image loads it displays the first picture saved in cache , and i have no idea how to resolve this.
I was using BLOB before but the application became too slow after adding lot of data.
Thanks for help.
Use signature
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(new StringSignature(UUID.randomUUID().toString())) //use this
.into(imgView);

Persistent/Offline Image cache in Android

I am trying to figure out the best and the most efficient way of caching images to the disk, such that they would persist even after app is killed and re-launched in airplane mode. Consider the following use case:
Open the app and get all images and display them in their respective ImageView's
Kill the app
Put device in air plane mode
Open the app again.
I am trying to get the images to persist in an offline cache so that they can be displayed in the scenario mentioned above.
I went through documentation for picasso and glide and it wasn't exactly clear if their disk caching would work in this case.
Is there a way to do this using picasso or glide? I am trying to avoid having to write a custom implementation for storing this in SQLite etc.
Glide will do this for you by default without any extra work on your side. You can also customize what versions of the requested image to store in the cache.
One important thing you need to consider is if the URLs that you use Glide to fetch images from are available offline otherwise you will need to have some way to cache those as well so that you can initiate the Glide calls when you are offline.
You can see how I did it in this project: https://github.com/KhalafMH/popular-movies-android.git
To read about how to configure Glide caching see:
http://bumptech.github.io/glide/doc/caching.html

How to load image from same url using glide or picasso in android?

Hi I have an wired issues stopping my development. I am working on the application similar to facebook having posts, comments likes and chat functionalities. In each functionality I need to load the user's profile picture. I can able to load the profile pictures when I was trying to login for the first time using glide libary. But when users changes the profile picture server is returning the same url but different image. How to invalidate or load image when it was updated and what are the best ways to handle this scenario?
Your answers are valuable to me. Thanks in advance.
like this:
Glide.with(MainActivity.this)
.load("URL")
.asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.man_default)
.into(imgUserImage);
For Picasso you can use both MemoryPolicy and NetworkPolicy
Picasso
.with(context)
.load(url)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageViewFromNetwork);
refer this its well explained here!
I think disabling cache strategy will be helpful for you as your url is same so
DiskCacheStrategy.NONE
From Here
difference between the enum parameters for the .diskCacheStrategy() method:
DiskCacheStrategy.NONE caches nothing, as discussed
DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one
DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)
DiskCacheStrategy.ALL caches all versions of the image
try using:
Glide.with(context)
.load(url)
.placeholder(R.drawable.ic_profile)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(ivUserProfilePhoto);
here is a relevant Question
I am glad to give you suggestion over it.
First, you should remember every profile has unique URL and it should be immutable on the server.
These issue is more sort of on server side because you never know on client side when other users changed their profile pictures but server knows.
Probable solution we have done for it
Whenever any user changes in profile picture we saving these picture on server with his timestamp and it attached to it's URL.
eg : http://image.pngtimestamp=1336t78387
So, now you have different URL for the user profile you don't have to change it on client side.
Only thing you have to take care for it is how you will get these updated URL for the specific user.We are requesting for URL to server whenever we chat with other user or see his profile. so every time we get the latest pic.
Even you want to improve performance for it you can request for updated images URL at the start of your application when user launch app so server can return you all the images after given timestamp.
Hope these helps you.

Storing images from api into room db

I want to know the best way to store images from and api into room db. I'm making a sport application where I receive data and images from an api. When I'm in online mode, the images are loaded using the urls provided by the api but when offline, images should be stored and retrieved from the database in offline mode. I want to know how to convert that url of image to an image and load it in offline mode.
PS : more than 100 images will be saved, does room db fit with that? Thank you.
store your image in cache using picasso
first add this to your gradle.
implementation 'com.squareup.picasso:picasso:2.71828'
then you can call the image from your url and store it in your cache.
Picasso.get().load(YOUR_IMAGE_URL).error(R.drawable.error_img).placeholder(R.drawable.loading_image).into(YOUR_IMAGEVIEW);
Picasso will only download the image once, and if you call the same url again it will be redirected to your cache instead of downloading it again(more or less).
note: check the profilier to see the data consumption.
To continue with L2_Paver's answer.
You can also check Glide for the same purpose. You might want to consider the pros and cons of each libraries which serves well for your purpose. This answer might give you some more insights about the comparison.
Picasso v/s Imageloader v/s Fresco vs Glide

Categories

Resources