I'm downloading some text data by issuing a HTTP GET request to server. I want to save downloaded text file to re-use it on request if it has already been downloaded. But I want to keep my data private, so that no other apps could access it. On the other hand, it would be OK if Android removed that files if there's not enough disk space.
So, my question is - should I store downloaded content in App Data folder or in cache folder? Is there any difference between two?
First, I used to save files in App Data folder, using a method like
public void save(String fileName, String data) {
FileOutputStream fos;
try {
fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Using this method I can set Private mode for my files so that no other apps could access them. But then I thought about moving files to cache directory, I need to do something like
private void save(String filename, String data) {
File cacheDir = new File(mContext.getCacheDir(), "app_directory");
cacheDir.mkdir();
try {
FileOutputStream fos = new FileOutputStream(new File(cacheDir, filename));
fos.write(data.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I cannot set Private attribute to my files anymore, so, as I understand, any application will be able to get access to my data. Am I right?
Maybe there's a way to make files in cache directory private? Or it doesn't really matter where to save files to?
Your cache stores files downloaded directly from the websites you visit—fonts, images, that kind of thing. The files in your cache aren't that different from the files in the cache of someone else who visits the same websites as you.
Caches are generally used to keep track of frequent responses to user requests. It can also be used in the case of storing results of long computational operations. Caching is storing data in a location different than the main data source such that it's faster to access the data.
Cached data reduces load times, but that data has to go somewhere, so it takes up some space on your device.
Usually, "data" is used for account info, settings, saved activity data, game scores, and whatever is need to be kept permanently. Clearing data will make your app "forget" everything like you have reinstalled it. "Cache" holds all information and files that can be retrieved somehow (from the internet, or other way).
Both the CacheDir
and the FilesDir
are app specific, and can not be accessed by any other app.
Both of these however can be accessed if the user has rooted their device.
The CacheDir is for temp files, that may be deleted if so required to free up space by Android OS. The Files dir will not be cleared unless explicitly done so by the app, the user, or if the app is uninstalled.
This is covered in the docs:
http://developer.android.com/guide/topics/data/data-storage.html
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.
Data in the cache can only be accessed your app (if its not rooted, but thats a user choice to minimze security)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With