Unfortunately, files stored in Android/data/<package_name>/file_folder
cannot be completely hidden from the user, especially on devices where the user has access to this directory via a file explorer or a computer connection. However, you can achieve better security by storing sensitive files in the application's internal storage, which is private to your app and inaccessible to normal users unless the device is rooted.
The internal storage path is:
/data/data/<package_name>/files/
Here’s how you can save and read files in the internal storage:
Writing Files: Use FileOutputStream to write data to internal storage.
String filename = "myfile.txt";
String fileContents = "Hello, World!";
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE)
fos.write(fileContents.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
Reading Files: Use openFileInput to read data from internal storage.
FileInputStream fis = openFileInput("myfile.txt");
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String content = stringBuilder.toString();
For larger files, consider using the getFilesDir() method or caching data with getCacheDir() for temporary storage. Avoid relying on external storage for sensitive information.