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.
Related
I have a scenario where i have to keep few images in offline mode. Now i am confused here that should i rely on Glide's cache or I have to put those images in Internal Storage?
Hopefully Glide will work, but need to know if there exists any limitation if i go with Glide. Here word cache is causing confusion.
Here is the information about underlying memory usage by Glide.
Glide uses DiskLruCacheWrapper as the default DiskCache. DiskLruCacheWrapper is a fixed size disk cache with LRU eviction. The default disk cache size is 250 MB and is placed in a specific directory in the Application’s cache folder.
Further underlying complexities of Glide are here. Hope confusion about the max memory Glide uses is cleared here and if customization is needed, that can also be achieved by following the link mentioned.
here the scenario
1: Open android app with glide, then glide download the image and save it in cache.
2.if app is close then reopen, then the URL is same, glide load the image from cache
3:While not connecting to internet,when open the app, i want to make glide display random image from cache, it will be nice if glide can list all the URL from cache
how to make step 3 work?
You can use DiskCacheStrategy.
Set of available caching strategies for media.
static DiskCacheStrategy ALL
Glide uses memory and disk caching by default to avoid unnecessary
network requests.
Glide.with(contextOBJ)
.load("IMAGE_LINK")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewOBJ);
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();
I'm using Picasso to initially download images of products to my app from a server. Since the images aren't changing nearly at all for a certain product, I save the images to the disk from the ImageView. In the app I have to display these images all over again in different sizes. To do so I'm using Picasso again, loading the saved file into an ImageView and do fit().centercrop() so I don't get any OutofMemory issues.
Since Picasso is also capable of using OkHttp's cache and doing the caching by its own, I want to know:
Are there any advantages about letting Picasso do the caching, over saving it to storage manually and handing it over again to Picasso later?
And would it be a good way to store the shrinked image (after using .fit()) as a new file so the calculation hasn't to be done all the time.
I have a question about mechanisms used by Picasso for downloading and caching images.
How does Picasso download an image? I know that it is using in sample size. Am I right? If image on a server is 1000x1000 but ImageView is only 400x400 then it will be download only 500x500 image and it will be cached. Or maybe it will be downloaded in full resolution and then scaled to a specific size.
Here is actual code I am - and I'm sure many more people are - using
Picasso.with(context).load(url).fit().centerCrop().into(imageView);
Picasso has no way of knowing it should download only 500*500 pixels. The fit() and centercrop() methods will make it fit even when picture is bigger than needed.
You can browse source-code of Picasso at: https://github.com/square/picasso.
Downloading images
You can see that Picasso downloads images with implementation of Downloader interface. It uses default downloader named OkHttpDownloader, which utilizes OkHttp library. When it can't be loaded, Picasso uses UrlConnectionDownloader.
Recognizing size of the images
Picasso doesn't know size of the images before download. If you are developing back-end server, you can specify size of the images in some way, so your mobile application will know it by performing a concrete request, but it can't be recognized by Picasso itself. Picasso has to download image in a full size and then this image can be cropped or resized by this library.
Cache
We can find the following information about Cache in the Picasso documentation placed in source code:
Picasso instance is automatically initialized with defaults that are
suitable to most implementations.
LRU memory cache of 15% the available application RAM
Disk cache of 2% storage space up to 50MB but no less than 5MB. (Note: this is only available on API 14+ or if you are using
a standalone library that provides a disk cache on all API levels like
OkHttp)
Three download threads for disk and network access.
It explains usage of cache in this library quite clearly.
I'm just not sure, if Picasso stores images before transformation (resizing, cropping, etc.) or after transformation in cache. First option seems more reasonable for me, because we decide to apply a different transformation later, so we may want to keep original image.