0

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();

    }


}
  • please share the screenshot of the logcat, while you click the button to write to file. – Tanuj Yadav Sep 23 '17 at 14:55
  • I found the logcat, ran my code and re-edited my message to include the screenshot of the log, is this what you are looking for? Thank you for prompting me, I thought that the log window in the Android Studio editor was where I would see errors. I still can't really make sense of it though – galluccinator Sep 23 '17 at 15:04
  • FileOutputStream fileOutputStream = new FileOutputStream(file); this line is generating filenotfound() exception – Tanuj Yadav Sep 23 '17 at 15:07
  • can you manually locate the file in storage. If not then try to run the app on real device, not emulator – Tanuj Yadav Sep 23 '17 at 15:09
  • I cannot find the file. I can try borrowing a friend's android to test it, as I don't have my own. is it an emulator issue? – galluccinator Sep 23 '17 at 15:14
  • seems to be a emulator issue. Try it on android, and tell me the results. – Tanuj Yadav Sep 23 '17 at 15:16
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Sep 23 '17 at 15:41
  • It didn't work on a real phone, but the new permissions issue is likely the problem. I will try to resolve, thanks! – galluccinator Sep 24 '17 at 01:48
  • Following the new standards worked, thank you!!! – galluccinator Sep 24 '17 at 19:44

0 Answers0