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.
Related
My requirement is to cache the files securely in android internal/external storage, where apps other than my app should not view/access the documents I store.
Current implementation:
Currently, the app uses context.getExternalCacheDir() as a base directory and followed by respective folder structure to cache files. The problem here is, any user can view the files stored by just navigating through the path using some File Explorer apps.
We can use context.getCacheDir() or file directory,
There are limitations in using it, as it has less space and the platform might automatically delete files when it wants space for other operations.
Required Implementation:
Encryption/decryption would be one way yet, please suggest other possible ways to cache the files securely, so that users cannot view/access using other external applications.
as it has less space
That is not true for most Android devices created in the last 8 years.
the platform might automatically delete files when it wants space for other operations
That also holds true for getExternalCacheDir().
please suggest other possible ways to cache the files securely, so that user cannot view/access using other external application
Use getFilesDir().
I making an application with phonegap/cordova where I need to keep a lot of files up to date. Some files (mainly images) will need to be erased in time, and some new ones will get downloaded. The thing is, in Android, to manipulate those files, it seems I need to have them on the sdcard; so I copy the files the app starts with from my assets folder to the sdcard. It just seems like a waste of memory space.
Do you know if is there anyway I can start with the app having those files the app starts with already inside the sdcard? or at least somewhere I can delete them later?
Thank you.
Files that are delivered to the device as part of your APK will be stored in a form that cannot be modified by your application (other than by updating to a new version of the apk).
If you copy the files out of the APK into the private internal storage area or the external storage area, those copies can be modified, but the originals inside the apk will remain.
The most efficient solution may be to not put these files in your apk, but have your app instead download them separately on the first run, using whatever mechanism you wanted to use to change them in the future.
(Some people object to this feeling that such files are less secure against unauthorized use, but as the contents of an .apk are trivial to extract this is not a strong argument. Needing to maintain a server to download from is a slightly more substantial objection.)
You do not need to store the files on the SD Card. Each app has its own internal storage that is not accessible by any other apps. For more information see the official docs: http://developer.android.com/guide/topics/data/data-storage.html
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.
I'm creating a simple Android App, it will be a map with a number of points marked on it. These points will sometimes change, based on an XML file hosted on the internet.
To reduce the initial load time my intention is to distribute a serialized List of these points with the application that can be updated in the future.
At first my intention was to distribute this serialized file as an 'asset'. This way I could just generate the file and drop it in to 'assets'. However, this does not work because (from what I can see) it is not possible for me to overwrite these assets.
The second option was to use the internal storage, however (from what I can see) I can't distribute this file as 'internal storage'.
Is my only option to distribute my serialized List as an asset and then on the initial load copy it to the internal storage? The files only going to be around 50kb but it seems unnecessary to have 2 copies of the same file (1 of which will eventually become outdated) as part of the same application.
How about always first checking the internal storage and if your data does not exist there read the 'asset' version.
Then you will have a syncronization job that will download the updated file (when available/updated) and put it in internal storage. But yes I guess you will be stuck with always having the original file there.
I have some questions about the cache directory in android.
Does anyone knows the limit for each app's cache directory?
Also what will happen if there is not enough disk space to cache data? All I know is when the device runs low on storage, the files here (cache directory)will be the ones that get deleted first and each app has its own cache directory. Now, are the files of other app's cache directory will be cleared to accommodate my app's request to add files in the cache dir when there is not enough storage?
There is no per-application limit for the cache directory. While Linux has a quota subsystem built-in, it is not used by Android. Applications share the cache filesystem without any real constraints. As it fills up, the Device Storage Monitor sends out an ACTION_DEVICE_STORAGE_LOW broadcast and calls a method of the Package Manager Service that sends a "freecache" command to the Install Daemon. The installd process then walks through the cache directory deleting files without regard to the owning application or modification date until the amount of free space available is above some threshold.