Java.Io.Filenotfoundexception: /Storage/Emulated/0/Notes/Fact50Result.Txt: Open Failed Enoent (No Such File or Directory)

java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory)

As discussed in the comments, you need to request permissions at runtime to allow your app to write to external storage. You can check if your app has the permission already by using checkSelfPermission(). For example:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the file write
} else {
// Request permission from the user
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
...
}

In order to handle this permission request, you'll also need to implement onRequestPermissionsResult() from OnRequestPermissionsResultCallback, for example:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 0:
// Re-attempt file write
}
}

Read the documentation for full information.

Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory)

Problem Solved

I used SharedPreferences of Android.
I stored the data in MainActivity and take in in my Class as follow:

  • MainActivity

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("string_id", InputString); //InputString: from the EditText
    editor.commit();
  • In my Class to get my data

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String data = prefs.getString("string_id", "no id"); //no id: default value

java.io.FileNotFoundException caught when processing request: /document/raw:/storage/emulated/0/Download/mypdf.pdf (No such file or directory)

I've just fixed a NumberFormat crash in code snippet from this brilliant answer. You can find my answer with code here.



Related Topics



Leave a reply



Submit