Okay so I've noticed that even though I use the correct path for the cache folder Android doesn't register the content in the folder so the user can't delete the cache content by going into settings -> programs -> administrate -> select program -> clear cache. The folder is deleted properly on uninstal but not if the user actively try to clear the cache. This is not a major issue but its still a minor problem because the user don't get a proper idea of how much space the application uses at the SD card.
Is there anything I as developer can do to update these values or am I doing something wrong somewhere else?
From the docs:
Saving cache files
If you'd like to cache some data, rather than store it persistently,
you should use getCacheDir() to open a File that represents the
internal directory where your application should save temporary cache
files.
When the device is low on internal storage space, Android may delete
these cache files to recover space. However, you should not rely on
the system to clean up these files for you. You should always maintain
the cache files yourself and stay within a reasonable limit of space
consumed, such as 1MB. When the user uninstalls your application,
these files are removed.
I guess you should handle the removal of yourself. If you want to remove the content if the user cleans the app data create a sharedPreference and clean the cache when the app starts and that preference is not defined.
Related
I am reading through the Android developer docs on saving data and they mention that when saving a file to internal storage you can use either getFilesDir() or getCacheDir(). When would it be appropriate to save to the cache directory, and what type of data is best saved here?
The getCacheDir() should only be used for data that is not essential for the running of the app. In other words, you can store data which if unavailable, can be fetched from the server. The reason for this is that the system may delete all the files in the cache dir to reclaim storage space.
http://developer.android.com/training/basics/data-storage/files.html
Use the cache directory to save temporary files. For example, if you're writing a social app (like instagram) and during runtime the app loads user's pictures, you can store those pictures on the cache directory, so if the user resumes the app while offline, he will still be able to see the last pics.
Make sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB.
Background
On android, you can create temporary files as such (link here and here for documentation) :
final File temp = File.createTempFile("myFile", ".tmp", context.getCacheDir());
I'm developing an app that uses temporary files and when it's done with them (and on start of the app), it deletes them.
The question
The documentation says :
When the device is low on internal storage space, Android may delete
these cache files to recover space.
Does it mean that files can get deleted even while my app is still running? In which cases would android be allowed to delete the files?
In other words , can I assume that as long as I have the app running , android won't delete the files by itself, even if other apps create their own temporary files?
You will can test it. Create new app, app write data to file and than internal stroge full. When internal stroge full Android delete applications cache directory which app store above 1Mb. If applications cache dir trashed, Android deleted even while the app is running. Otherwise while app running Android never deleted cache files.
This is the original link shared by developer.android.com
http://developer.android.com/reference/android/content/Context.html#getCacheDir()
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.
Ok. trying to understand how Android handles downloaded images. So a few questions:
1) Is it possible to download and save an image to the getCacheDir()?
2) Can this image be viewed from the cache directory?
3) If yes to 2), what is the action that will clear up the cache?
4) Must I download the image to somewhere before I can use the ACTION_VIEW intent to view the image?
5) If it is in the cache, how do I "save" it? Meaning put it in a more permanent directory and not the cache.
In summary, I'm trying to achieve user downloads an image from somwhere, sees it in the image viewer and chooses to save it or not.
Currently my understanding on this is I have to download and save it regardless before viewing it on an imageviewer with no choice to save it or not.
If I am wrong what is the approcah to achieve download, view, choose to save?
Ok, from your questions..
1) Yes
2) Yes (I think only for your application.. May be)
3) You have to do it manually, using delete files form getCacheDir() or either by navigate
to settings/Manage application clear cache data option..
4) This never tried, but I think you have to do it, (Bco'z I think cacheDir is private for your application)
5) Using File operation copy to another internal / external storage location...
Note:
getCacheDir ()
Returns the absolute path to the application specific cache directory on the filesystem.
These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted. 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.
I am trying to decide where to store images that are sent as part of instant messages coming in to an app. These messages are viewable in a conversation history view for sometimes a significant period of time after their original receipt. You can imagine any number of other use cases that would have a similar requirement, so the question here is on the "best practice for storing an indeterminate quantity and size of images"
Assumptions
SQLite storage is clearly a bad option since the image size is not
bounded.
It is neither desirable nor undesirable that these images be publicly available to other apps or discoverable by MediaScanner. We are assumed to be perfectly neutral on this point...
This leaves two parts to this question:
1. External Storage
It seems like external storage is to be preferred when available because it is likely to have more room than anything else:
The documentation says the following:
...use getExternalCacheDir() to
open a File that represents the external storage directory where you
should save cache files. If the user uninstalls your application,
these files will be automatically deleted. However, during the life of
your application, you should manage these cache files and remove those
that aren't needed in order to preserve file space.
Unlike internal storage cache, there is no statement made about the automatic reclamation of space on external storage by Android. Still the word "cache" makes me nervous.
Question 1: Do these files remain until explicitly deleted regardless?
Question 2: Is there any other external storage other than the cache that is automatically deleted upon app uninstall AND is preferable to the external cache for some specific reason?
2. Internal Storage
Clearly not every device has external storage, so there needs to be a provision for internal storage.
Question 3: Is the only practical difference between the internal cache retrieved through getCacheDir() and files created with openFileOutput(FILENAME, Context.MODE_PRIVATE) that Android may delete files in the cache directory when under pressure for storage space?
Do these files remain until explicitly deleted regardless?
I haven't read the code, but the javadoc explicitely says
The platform does not monitor the space available in external storage, and thus will not automatically delete these files. Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir().
Is there any other external storage other than the cache that is automatically deleted upon app uninstall AND is preferable?
None that I know of.
practical difference between the internal cache retrieved through getCacheDir() and files created with openFileOutput?
It's just a facility method, AFAIK
What must I do that Android can delete my cache files on the sdcard with the GUI?
I saved some data in /sdcard/Android/data/my.package.name/cache but Android tells me that my app has 0B cached data. That is wrong!
You should never hardwire paths -- for example, your path shown above is incorrect for most Android devices, as /sdcard is not where external storage resides. You should be using getExternalCacheDir() for external cache and getCacheDir() for internal cache.
Bear in mind, as the documentation states:
There is no guarantee when these files will be deleted. 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.
(emphasis theirs)
I could not find valid source of this but after multiple tests I am most certain that external storage does not show up under this "clear cache" button.
It's on you to clear cache manually