How to Move a Sensitive Android File to filesDir and Verify the Result
For a sensitive file, verify both that saving succeeds and where the data is actually written. This article walks through moving one persistent file to internal filesDir: creating it, reading it back, checking it after a restart, and deleting a known previous external copy. All examples use a wholly fictional app called “Personal Journal” and synthetic records you own. The outcome is a verifiable location for the working original, not a promise of complete data protection.
Understand the access boundary
Android documentation recommends storing sensitive data in internal storage regardless of the target system version. Sandboxing restricts access to the owning app; the documentation includes a separate caveat for rooted devices. Treat the internal directory as an access boundary with limitations, rather than an unconditional security guarantee.
Ordinary persistent app files belong in the directory available through context.filesDir. The getExternalFilesDir() call is for persistent files in external storage. Having Files in both API names does not make these storage locations interchangeable.
Prepare one fictional file
In “Personal Journal,” use one file, journal.dat, with a fixed, simple name. Its contents are a predefined set of bytes containing fictional records, such as “Entry A: test visit; entry B: test note.” No real personal information is needed for this check. Keep the original byte sequence as the reference for comparing the entire readback result.
The following are original editorial acceptance tests proposed for this example. They are not an official Android procedure. To test a new write, start by creating the file within the app; to test a migration, first prepare your own synthetic copy at the known previous external location.
Direct writes to filesDir
Use openFileOutput(name, Context.MODE_PRIVATE) to write the file, with journal.dat as name. This method provides an output stream for a file inside filesDir. Write the prepared bytes and close the stream. Reading and writing in the app’s internal directories requires no system permissions.
On Android 7.0 (API 24) and later, you must pass Context.MODE_PRIVATE when calling openFileOutput(); otherwise, a SecurityException occurs. Also check every other place that saves this particular file: writes to the working file must target the internal directory, including writes after a subsequent change to the synthetic note. Fixing one call is insufficient if another save path still uses getExternalFilesDir().
Confirm the directory and contents
The first acceptance criterion is the actual location of the created file. Obtain it through the app’s API and compare its parent directory with context.filesDir. Do not substitute a presumed absolute path or infer the storage type from the physical location of the memory. The check must show where the app directed this specific write.
The second criterion is readback accuracy. After closing the output stream, open journal.dat through openFileInput(), read its contents, and compare every byte with the reference. Both the length and every byte must match. The absence of an exception, the appearance of a filename, or a “Saved” message alone confirms neither the correct directory nor the integrity of the contents.
Read the file again after a restart
Terminate the fictional app’s process and launch it again without uninstalling the app or clearing its data. Before any new write, open the same file from filesDir and compare the bytes read with the original bytes again. The acceptance criterion is a complete match without first recreating the file.
This result confirms that the file persisted through the restart you tested. It does not mean the data exists independently of the app: files in app storage are deleted when the app is uninstalled. In the test report, record creation, the directory match, the first read, and the read after restart separately.
Complete the migration of the previous external copy
If the file was already stored externally, first move its synthetic contents into filesDir, close the output stream, and confirm an accurate readback. Only after this check should you delete the known previous external copy. Then, with external storage available, check that the file is no longer present at its previous location.
After a restart, read the internal original again and check that the external copy has not reappeared. If the external storage medium is unavailable or deletion failed, the old file’s absence has not been confirmed: the migration check cannot be considered complete. This test covers one known copy and does not prove guaranteed erasure from all storage media.
Fix priorities and verification limits
First correct the location of writes to the working file, then confirm the directory and byte accuracy, and then test a restart and finish deleting the previous copy. An internal directory does not itself imply encryption: the documentation recommends encrypting sensitive data regardless of storage location. Backup policy also needs separate consideration.
This article does not cover WebView caches, temporary files, backup and device transfer configuration, FileProvider, download validation, ZIP extraction, serialization, key management, or the IV lifecycle. If your product has a website or web component, you can start with a free website or web-product audit. The Android checks described here remain a separate task for the development team.