Upon request, here is the log file when I press the button I have scoured the internet, this forum, and youtube to try to figure out how to write a simple txt file to external storage. I found this great tutorial on youtube:
https://www.youtube.com/watch?v=kerqarY7_wQ
And I followed his code to the tee. However, when I call the method shown below, nothing happens. No exceptions are thrown, the file isn't created, and I don't see any of my fail-safe messages pop up on the screen through Toast. What is happening?!?! I made sure to add the permissions to the manifest file, and the buttons definitely call the correct methods. For reference, I tried his tutorial on writing to internal storage and it works perfectly. I don't understand how it can just not show any messages, because even if it couldn't write to storage it should have told me. The code is below, I would really appreciate any help.
//Permissions files in Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
//Method in main java file
public void writeExternalStorage(View view) {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
File root = Environment.getExternalStorageDirectory();
File Dir = new File(root.getAbsolutePath()+"/MyAppFile");
if(!Dir.exists()) {
Dir.mkdir();
}
File file = new File(Dir,"Test.txt");
String message = editText.getText().toString();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(message.getBytes());
fileOutputStream.close();
//the editText variable is the text field on the app.
editText.setText("");
Toast.makeText(getApplicationContext(), "Message Saved!",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "SD Card not found.",Toast.LENGTH_LONG).show();
}
}