Android image caching - hard and soft HashMaps question - android

What I'm trying to do right now within my app is modify the ImageDownloader class that Google put out last year in one of their tutorials that asynchronously downloads and caches images for ImageViews without leaking the context.
In other words, since I'm using a global cache singleton object which provides references to my Bitmap HashMaps, I just need to know: since I have to cache images separately depending on certain aspects of my app (client requirement), should I have pairs of hard and soft HashMaps for each of those types of Bitmaps, or would it be more efficient to have only one soft HashMap in which the other hard caches move their files to when they are pressed for space?

I've encountered a similar problem on an app we worked on. We hard cache images we pull in an SQLite blob. The wrapper will check the cache's existance, or go pull over the network, so even if someone clears the app data, it'll work (a bit slower until images are cached again). Since it's in SQLite the app data can easily be moved around to the SD card and back without having to worry about file paths being changed.

Consider using something like:
ConcurrentHashMap<String, SoftReference<Bitmap>> image_cache =
newConcurrentHashMap<String, SoftReference<Bitmap>>( 1 );
For the memory portion of your cache. The SoftReference's will be garbage collected as the device needs memory. This will allow your application to keep as many images in memory as possible without causing memory issues.
You can choose to back this cache with a file cache either on the SD card with a root path using:
Environment.getExternalStorageDirectory();
or using the cache space provided by android using a path of:
context.getCacheDir();
The cache space is private whereby the SD card images could be retrieved by a user or modified by a 3rd party program. The cache space is on the internal storage of the device and it shows up in the application manager statistics. The user can also clear this cache easily from their settings->application manager screen.
You will need to fill in the algorithm that checks the memory cache first, checks the soft reference if found, checks the file system if not found, and then finally fetches from the network, saves to a file, and puts it in the memory cache. You can then add additional requirements for the cache based on your client requirements on top of this structure.

Related

Self monitor cache directory

I am currently storing bitmap images inside my cache, each bitmap can be upto 3mb in size each. I am using getCacheDir() however, after reading androids documentation, I found this:
Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.
So I am considering switching my cache to using getExternalCacheDir(), but I am abit uncertain about this:
The platform does not always monitor the space available in shared storage, and thus may not automatically delete these files. Apps should always manage the maximum space used in this location. Currently the only time files here will be deleted by the platform is when running on JELLY_BEAN_MR1 or later and isExternalStorageEmulated(File) returns true.
I would like to have full control of the cache dir, because if files are randomly deleted this could really affect the running of my application. I have already set an upper limit for the size of the cache so it will never exceed a certain amount of space. How can I now prevent the system from monitoring and deleting from this cache as it pleases?
thanks
Don't think that cache is permanent storage. You mustn't store there files that you rely on. Cache used for storing data that you don't really want to reload or recreate, but you can do it if needed (as with loading pictures from web. Much faster to load from disk, if already done, but can be done without it). If you really depend on this files use getFilesDir() this will route you to specific app directory, that wouldn't be erased if no memory (but still can be erased by user in settings). Also, if you have data that is static and you need it always, you must store it in the assets folder, all other data must be considered as temporary (any time user or system can erase it) and checked for existence.

What is difference between Android cache memory and Android apps cache memory

Could anyone please tell me what is android cache and apps cache because when we talk about cache clean apps then what it do, is cache clean concept is cleaning apps cache or else like memory management Primary storage,RAM,Cache are different and as i know cache size small because it is very costly. so i want to be clear that what exactly the concept of cache in android OS.
The cache is basically a temporary storage area for an application while it's running -- it can store things like temporary images downloaded from the internet. Clearing it shouldn't really have much of an effect from a user perspective. Clearing the data, however, will remove any user data, databases, etc -- this includes things like account settings, persistent preferences, etc. Clearing the data should revert the application back to it's state as it was when you first installed it.
As an example, assume we have a Twitter application. Clearing the cache might delete any profile images for people you're following, so when you look at your timeline again, they'll have to be retrieved from the internet again. Clearing the data would remove any information at all, including your login information, who you're following, whether or not you want to be notified of new tweets, and things like that.

android Download hundreds of images and store them to be used on revisit or when the internet is not there

I have a listview and each listview item when clicked opens a gallery (conating 30 + images ) to be downloaded (using the urls).I don't want to re - download the images on next visit and also want the images to be present when i click on list in offline mode.
I went through several links in SO and am a bit confused on which approach to follow:
1> Use bytearray and store the images in sqlite db.
2> use context -> getExternalCacheDir() to store it on the
external memory (this wont work on devices with no external
memory.)
3> Use SoftReferences as suggested in following link
http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html
Please help to guide me on the best approach to handle around 1000 + images which works in offline mode as well.
1000 images.....I sure hope they're thumbnails or you're going to end using a lot of storage space.
You should really only cache what you need and what images are used repeatedly. Both options 1 and 2 are dependant on the phone even having storage space, if there's no space then you won't be caching much to disk. 3 won't store images permanently, if the app is put in the background and gets killed and so will the cache.
It's hard to say what the correct decision here is without knowing the inner workings and requirements of the app.
Just going by what you want then I would say go with several solutions
If there's external storage write to that, if not use internal storage, if there's not enough space internally use the soft references.
I have always found that out of the box caching solutions never fit quite right with my apps and wind up writing my own cache. Sorry I can't give a better answer.
PS. If you're not careful with 3 you might download too much and run into an OutOfMemoryException.
PPS. 3 can also throw a RejectedExecutionException which is caused by too many async tasks being started at once (eg scrolling quickly through a listview that gets an image via asynctask)

Best Practice when Caching files in Android

I currently have my app caching image files in the cache sub-directory for the application. The images are used in a ListView and stored in a HashMap of SoftReferences to Bitmaps.
So my question is this, what is the best way to cache these image files without inflating the space my application uses AND remains responsive from a user standpoint.
Things I am concerned about:
I know the user can clear the cache and that it is done automatically when space is low on internal memory, but I feel most users will see a several MB app and uninstall it. Also if the space is constantly low, my app will just keep downloading the images, making it appear slower.
Most devices have an SD card pre-installed, but what should I do when it is not inserted? The SD card may also be slower compared to internal storage, affecting my app's performance.
Should I include an option to choose the location of the cache?
Should I attempt to manage the size of my cache (be it in the /cache or /sdcard) or just forget about it?
Thank you for your time (its a long one I know) and please post any relevant experience.
I can't offer you a comprehensive set of best practices, but I can offer what I've learned so far:
Managing your cache is a good idea. My app's cache is such that I know that I'll never need more than a certain number of cached files, so whenever I insert a new file into the cache, I delete the oldest files until I'm under the limit I have set. You could do something similar based on size, or simply age.
Caching to the SD card, if it's available, is a good idea if your cache needs to take up a lot of space. You'll need to manage that space just as carefully, since it won't automatically clear that space for you. If you're caching image files, be sure to put them in a directory that begins with a dot, like "/yourAppHere/.cache". This will keep the images from showing up in the gallery, which is really annoying.
Letting the user choose the location of the cache seems like overkill to me, but if your audience is very geeky, it might be appreciated.
I haven't noticed a much of a penalty when caching to the SD, but I don't know how your app uses that information.
Everyone has good ideas. I like the idea of using SoftReference's, although I'm not sure how often those get cleaned up, as this varies so much from VM to VM. You might want to combine that with regular HashMap to prevent you entire cache getting cleared every few minutes.
EclipseLink has a few different cache implementations and pretty good documentation on them. You could probably take advantage of a few ideas from the implementation (e.g., LRU, MRU, etc.). e.g.,
hard cache
soft cache
combined hard/soft cache
Since you're tuning a cache down to the nitty-gritty, I would recommend tuning it to different devices based on the hard specs. This is normally bad design, but the scope of the hardware that your software runs on mandates it, IMHO. e.g.,
Detect the amount of available memory on the SD card. Most new smart phones come with multi-GB SD cards, and those are pretty hard to fill up with regular usage for most users. Use away! You can also detect the amount of space available on the SD card on startup, and increase/decrease the size of your cache on startup.
Detect the amount of available memory and configure your caches with that in mind. If a user is using a hardware-intensive application, I don't think they'll mind that it makes up 200MB of RAM and provides a very fast user experience, especially since they spent a lot of money to have a phone that has 1-2GB RAM.
Good luck!
Should I include an option to choose the location of the cache?
IMO: No, let make it more simplest as possible (Except you can include advance setting for expert user)
Should I attempt to manage the size of my cache (be it in the /cache
or /sdcard) or just forget about it?
IMO: This is optional, it is double sword: your more work on background will help user more convenience but also more bug prone
Use 3rd libs:
IMO using 3rd library as Picasso is better, it handle cache automatically by order: Memory cache -> Disk cache -> Network

What is the ideal place to cache images?

I am fetching a lot of thumbnails in my application from a remote server and lazy-loading these in a list view.
The image fetches run in a background AsynTask and when completed the fetched images are stored in a HashMap using SoftReferences.
This works just fine but when the images in the cache gets GC’d, the fetches have to be rerun.
I could have stored them on the SD card so there would be no re-fetching.
But I did not take this approach because of the clutter it would create on the SD card.
Are there perhaps alternatives to these sorta like temporary folders that can be cleared when activity/app finishes/exits?
What is the "ideal place" to cache images? Any advice, example projects, or pointers would be appreciated.
You should check files data storage options:
http://developer.android.com/guide/topics/data/data-storage.html
Files are being saved in the applications directory, so there is no mess anywhere. Files will be finally removed with the app. You can also implement some method which will clear the cache when the app starts (for example remove files older than 2 weeks).
You should be able to locate a SQLite DB on the SD card. This would let you have a fairly clean interface/cache space while keeping data off the very limited internal phone memory. You can specify the location of the DB by calling openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory). It may be more work than you'd like to put in, but you should be able to clear ou the DB when you application is unloaded with a line or two of code.
More from the documentation here:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Categories

Resources