0

I am making a notetaking app and I want to make a folder where all notes should be stored. I tried this, but it doesn't work:

public boolean checkPermission(String Permission){
    int check = ContextCompat.checkSelfPermission(this,Permission);
    return (check == PackageManager.PERMISSION_GRANTED);
}
private boolean isStorageWritable(){
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

public void saveNote(View view) {
    EditText TitleBox  = findViewById(R.id.titleText);
    EditText NoteBox = findViewById(R.id.noteText);
    String TitleText = TitleBox.getText().toString();
    String NoteText = NoteBox.getText().toString();
    if (isStorageWritable() && checkPermission(WRITE_EXTERNAL_STORAGE)){
        File folder = new File(Environment.getExternalStorageDirectory(),"MyNotes");
        if(!folder.exists()) {
            folder.mkdir();
        }
        try {
            File TextFile = new File(folder, NoteText+".txt");
            FileOutputStream fos = new FileOutputStream(TextFile);
            fos.write(NoteText.getBytes());
            fos.close();

            Toast.makeText(this, "FILE SAVED", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "FILE COULD NOT BE SAVED", Toast.LENGTH_LONG).show();
        }
    }
}

Also my app asks permission in Android manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

This is the error I get:

2022-01-05 11:56:06.236 28169-28169/com.antares_studio.mynotes W/System.err: java.io.FileNotFoundException: /storage/emulated/0/MyNotes/tsets.txt: open failed: ENOENT (No such file or directory)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mohak Jain
  • 1
  • 1
  • 1
  • Does this answer your question? [Error: open failed: ENOENT (No such file or directory)](https://stackoverflow.com/questions/36088699/error-open-failed-enoent-no-such-file-or-directory) – The Amateur Coder Jan 05 '22 at 06:41
  • 3
    Does this answer your question? [Can't create directory in Android 10](https://stackoverflow.com/questions/58379543/cant-create-directory-in-android-10) – Sujal Kumar Jan 05 '22 at 06:46
  • `if(!folder.exists()) { if(! folder.mkdir()) return: }` Display a Toast() too to unform the user. – blackapps Jan 05 '22 at 07:23

0 Answers0