Android Does admob use cache? How Cache works? - android

I have this aplication thats keeps building cache every time I open the app
In my app I load like 20 images from my website, but I don't save them to cache,
does the app automatically save this images to cache? If so, I don't think they load from cache, cuz I still have to have internet to load them
Admob in the other hand, if I don't have internet and I loaded the test ad before, it will load the testad.
currently I have the app to delete the cache everytime it starts, keeping cache low, but this doesn't seems smart.
My questions are:
Does admob uses cache of my app?
Knowing my app loads 20 images from my website, this images being
loaded affects my cache size even if I don't save them
programmatically to cache ?
is deleting cache expensive enough to have a dedicated thread to
do just that?

Does admob uses cache of my app?
Yes i think Admob caches its ads so they load faster. And Admob should be managing its own cache size. I don't think you need to worry about that.
Knowing my app loads 20 images from my website, this images being loaded affects my cache size even if I don't save them
programmatically to cache ?
No images or bitmaps will not be cached unless you do so, see this link for more info. I will recommend that you use the Glide or Picasso for image loading since they are memory efficient and abstract out most of the complexity.
Is deleting cache expensive enough to have a dedicated thread to do just that?
I don't think you should use dedicated thread. What you can do is override onTrimMemory and keep monitoring the memory level.When your memory level is critical or low you can delete the cache. Check this link for more info. In my opinion if you use Glide for loading your images it will already handle the memory very well and prevents OutOfMemoryError. Also, loads image much faster.

Related

Getting large number of images in recyclerView android

in my recent android project I have to get a large number of images from the web and show them in my main activity (something like explore part of the Instagram app). I used a recyclerView with gridLayoutManager of 3 columns and I have to get something like 700 images from URL. When I open the app and comment the getting image part, app work perfectly fine (the app get the information of each tile but doesn't display the images). But when I start setting image bitmaps the app start becoming laggy and crash after about a hundred images loaded.
What do you recommend me to do ?
And another question: Was using recyclerView a smart idea ?
Thanks for your reply.
Don't load hundreds of images. That requires a few things:
1)Don't store images in memory in the adapter. Store the url/resourceid/whatever of the image so you can load it on demand.
2)Use an LRU cache of images, limited in size and load your images through that.
3)Make sure that however you download images does not spawn too many concurrent requests, and that requests are canceled when no longer needed (when the view it would go into is recycled).
4)I'd suggest downloading the images and writing them to disk, then loading them from disk as needed. This will prevent you from having to keep the entire file in memory to decode it while downloading it, which will reduce your memory usage while downloading.
5)Do not decode the image on the UI thread. Do it on another thread.
6)If you don't need to display images fullsize, make thumbnails.
Images in a RecyclerView, especially if being downloaded need a lot of work to do well and handle rapid scrolling.
You should post some code here but seems you are asking for some efficient setup.
First of all, try using some image caching libraries like Glide or
Picasso. It manages and caches your images locally so you don't end up
making multiple requests for the same image.
This solves most of your problem and don't try to load 700 images
altogether and display use lazy loading means load first 10-20 images
first and when user scrolls make another API call for another 10-20
and so on.
Here is an article on how to use Glide and how it works.
https://futurestud.io/tutorials/glide-image-resizing-scaling

Android best way to load images from device storage

I am loading multiple images locally from devices storage. I am currently using Glide to load them. But Glide is very big library and it offers many features which I don't need at all, for ex. disk cache and loading from internet. Is there any better and more efficient library or another way to load images from devices storage?
The only functionality I need is memory bitmap cache, async loading from Uri and resizing just like in Glide.
You can use Picasso, its method count is ~700 compared to ~3200 of Glide, you can save lots of method count here.
I will recommend you do keep using image library, it does lots of cool stuff in the background, which requires lots of dev efforts.
Resizing of images according to view size
Caching in memory using LRU cache
Recycling of Bitmaps to free up heap once you are done with displaying
Disk cache, so that you can load faster on app restart OR eviction from LRU cache
Proven record of working on fragmented Android ecosystem
No need to take care of threads, thread pools
Also on top of if you are using Proguard (using minifyEnabled true in your build.gradle ) for code obfuscation, it will strip away methods which are not getting used in your apk

Android Glide alternate cache in memory or disk

I´m working in an App which does have images that are used quite often, however there are others download used only once.
Does Glide have any way for deciding on the fly which images should be stored only in Disk or only in memory?
As far as I´ve seen it does the cache depends on the configuration buy I´de like to be able to say by myself which ones should be in disk and which ones no.
You can use .diskCacheStrategy() to manually control whether and how an individual request is cached on disk and skipMemoryCache() to control whether an individual request is cached in memory.

Android: Retrieve images from Server

I am building an android application which requires displaying images as a flip-view which will be retrieved from server. I have considered two approaches
Retrieving images from server URL and then displaying OR
Storing the image in db at server(MySQL) and then retrieving it from sq-lite on android application
My question is, which approach will be better considering everything (performance, etc.)?
Any other better approach is also welcome :)
This depends upon your Application and products you are going to display using Images, If they are not updated frequently then it would be better to use caching for faster user experience but if data is regulatory updated and previous data is keep getting filtered then always load using network.
Another approach you can just store latest 10-20 entries in your Database and as soon as you opens the application he can see some data and new data gets downloaded, this approach keeps users engage don't leave them your app just because every time they see loading.
For Image caching purposes there various good libraries avaible which are stable solutions for multiple images downloading as well as caching purposes E.g.
UniversalImageLoader, picasso, Volley
A good approach could be store locally images locally on demand, and keep it on cache, if the image changes on future you can invalidate your cache and download a new image.
I have used in a project a library that helps me a lot with to download the image in background, allows you also load image from cache automatically if stored previously or invalidate the cache. The library is android-query and here is an example of how you could use it.
You store images locally and load then to your app from local storage (This happens in a background thread)
You update the local storage by fetching the images in background, when needed, and then trigger the load from local storage (This also happens in a background thread)
This way you won get ANR's (application not responding) because of slow or missing internet connection, and you will be able to show images without connection at all.

Android best way of caching images

I am making an attempt to build the most efficient way of caching images downloaded from the web for my app, a few years ago I tried to do this and could not find an efficient method of making this work until I stumbled upon the lazylist adapter found here:
Lazy load of images in ListView
this worked well until android 4.0 was introduced, at this point the app would crash after loading 10 to 20 images, as opposed to before where I could simply load up as many as I wanted without any issues, this I later found out was a result of the Ice cream sandwich having a set limit on memory usage per app, which didnt exist in Gingerbread 2.3 and below, I eventually decided to just clear the cache every 10 or so images to avoid crashing, however the user experience wasn't very good as a result of doing this and the app used tons of data as it was constantly redownloading images over and over again that were already viewed, I have since attempted to use an lru cache but this does not seem to work at all, especially when I leave the app and relaunch it the images are all released it seems, I need a better way of doing this and I have noticed that other apps such as instagram seem to have found a way to cache hundreds of megabytes of images, Im consistently having to manually clear the instagram cache in my settings because it seems that they are to able to store an infinite sized cache that seeming never ejects its contents, does anyone know how to build this kind of cache?
You can try using https://github.com/nostra13/Android-Universal-Image-Loader
library. This does most of the hardwork for you.
the way I wold suggest would be to store the images from the web to the sdcard or something then store the URI where the mage is located. The in your list just load the image from the uri.
If your images are big in size you should probably rezise the image before you save it so that you use less memory and it will load faster since the processing has already been done
BTW the memory limit always existed in android

Categories

Resources