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.