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?
android storage
Alexander Zhak
source share