I am trying to write a file to internal device storage on my phone (and on user's phones in the future). I was watching a video tutorial from 2016 (https://www.youtube.com/watch?v=EhBBWVydcH8) which shows how he writes output to a file very simply. If you want to see his code, skip forward to 8:23.
Anyway, I basically tried his code, then since that didn't work, I figured would search around.
Apparently, to create a file, I need these lines of code:
String filename = "textfile.txt";
File file = new File(filename);
file.mkdirs();
file.createNewFile();
On the second line, file.createNewFile(), I get the below error:
java.io.IOException: Read-only file system
at java.io.UnixFileSystem.createFileExclusivel
at java.io.UnixFileSystem.createFileExclusivel
at java.io.File.createNewFile(File.java:948)
etc......
And then, if I run my code just by using the lines of code from the tutorial, I get a Null pointer.
Code:
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(IDNum.getBytes());
fos.close();
System.out.println("Wrote STuff Outputtt?");
} catch (Exception e) {
e.printStackTrace();
}
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:199)
at com.lecconnect.lockoutdemo.FileManager.AddUser(FileManager.java:37)
Line 37 is the first line in the try/catch.
Please let me know if you require any additional information to assist me. Your replies are greatly appreciated.