Cache vs Data Storage for Downloadable Content

I upload some text data by issuing an HTTP GET request to the server. I want to save the downloaded text file so that I can reuse it on demand if it has already been downloaded. But I want my data to be private so that no other applications can access it. On the other hand, it would be nice if Android deleted these files if there was not enough disk space.

So my question is: should I store the downloaded content in the App Data folder or in the cache folder? Is there a difference between the two?

First, I used to save the files in the 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 the private mode for my files so that other applications cannot access them. But then I thought about moving the files to the 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 can no longer set the Private attribute for my files, so, as I understand it, any application will be able to access my data. I'm right?

Maybe there is a way to make files in the cache directory private? Or does it not matter where to save the files?

+8
android storage
source share
2 answers

Both CacheDir and FilesDir are application-specific and cannot be accessed by any other application.

Both of these, however, may be available if the user has rooted their device.

CacheDir is intended for temporary files that can be deleted if necessary to free up space from the Android OS. The dir file will not be cleared unless explicitly done by the application, by the user, or if the application is deleted.

+10
source share

This is described in the docs:

http://developer.android.com/guide/topics/data/data-storage.html

Saving Cache Files

If you want to cache some data rather than persistently, you should use getCacheDir () to open a file that represents the internal directory in which your application should save the temporary cache files.

When the device is located in a low internal memory space, Android can delete these cache files to restore space. However, you should not rely on a system to clean these files for you. You should always maintain cache files yourself and stay within a reasonable limit of the space consumed, for example, 1 MB. When a user uninstalls your application, these files are deleted.

Data in the cache can only be accessed by your application (if it is not implemented, but this is the user's choice to minimize security)

+1
source share

All Articles