I am currently running JUnit tests with a testing application on an emulator that is running my target application and verifying results. During the runtime of these tests, I have specific files that I would like to save to the testing applications internal, persistent storage, as described in this portion of the Android documentation.
This area of the documentation states that
Your app doesn't require any system permissions to read and write to files in these directories.
However, whenever, I attempt to create a file by following the documentation, I find that I do not have permissions to read nor write a file in that area.
I've attempted the following. I've run these commands in the after methods of JUnit rules which are executed in the testing APK, not the target APK.
First, I've attempted to follow the documentation and create a file using
val f = File(context.filesDir, "tmp.txt")
However, f.canRead() and f.canWrite() both return false, which suggests that I don't actually have permissions to read/write this specific file, despite what the instructions stated in the Android documentation I linked above.
Additionally, I've tried proceeding despite these signals and writing data, and executing f.createNewFile() throws an "java.io.IOException: No such file or directory" exception. Additionally, f.parentFile.mkdirs() appears to fail as well by returning false. Finally, I've attempted constructing a FileOutputStream(f), and that throws a java.io.FileNotFoundException with a cause of "android.system.ErrnoException: open failed: ENOENT (No such file or directory)".
I'm not exactly sure why I'm having such issues with saving files, especially when the documentation stated that I do not need to be concerned about the permissions of the application; however, if anybody has encountered any similar issues or has any advice, that would be greatly appreciated. Thanks!
Edit:
The documentation stated that the persistent, internal files should be located within the directory obtained through the filesDir property on the appropriate context object. As a result, because I'm performing instrumentation tests and would like to save these files in the testing APK, I get the context via
val context = InstrumentationRegistry.getInstrumentation().context
, and then, I'm using the context.filesDir attribute to determine what directory to placed the files under.
Also, I have attempted to create files via f.createNewFile(), and that returns false as well.
in yout AndroidManifest add this line