0

So I have no external SD card and I want to write the file in the External partition of my devices internal storage.

Here is my code:

private String filename = "SampleFile.txt";
private String filepath = "MyFileStorage";
File mySensorData = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
    , filepath);
    String myData = "";

private void FileWrite(String sensorReading) throws IOException {
        if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
            try {
                FileOutputStream fos = new FileOutputStream(mySensorData);
                fos.write(sensorReading.getBytes());
                fos.close();

                Toast.makeText(this, "File written to the external storage.",
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {

            Toast.makeText(this, "Unable to read the external storage.",
                    Toast.LENGTH_SHORT).show();
        }
    }

    //external storage discrepancies handler
    private static boolean isExternalStorageReadOnly() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
            return true;
        }
        return false;
    }

    private static boolean isExternalStorageAvailable() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
            return true;
        }
        return false;
    }

I am constantly getting the Unable to read the external storage. toast and this error on the monitor:

W/System.err: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)

Where is the problem? Thanks.

Jishan
  • 1,654
  • 4
  • 28
  • 62

1 Answers1

-1

You can't do that you need superuser privilges or root user check this post already answered.

Trying to create a file in Android: open failed: EROFS (Read-only file system)

Community
  • 1
  • 1
Sandy
  • 251
  • 1
  • 4
  • 10