Trying to create a file in Android: open failed: EROFS (Read-only file system)
I have tried this with and without the WRITE_INTERNAL_STORAGE permission.
There is no WRITE_INTERNAL_STORAGE
permission in Android.
How do I create this file for writing?
You don't, except perhaps on a rooted device, if your app is running with superuser privileges. You are trying to write to the root of internal storage, which apps do not have access to.
Please use the version of the FileOutputStream
constructor that takes a File
object. Create that File
object based off of some location that you can write to, such as:
getFilesDir()
(called on yourActivity
or otherContext
)getExternalFilesDir()
(called on yourActivity
or otherContext
)
The latter will require WRITE_EXTERNAL_STORAGE
as a permission.
Is there an easier way than writing it to a file then reading from it again?
You can temporarily put it in a static data member.
because many people don't have SD card slots
"SD card slots" are irrelevant, by and large. 99% of Android device users will have external storage -- the exception will be 4+ year old devices where the user removed their SD card. Devices manufactured since mid-2010 have external storage as part of on-board flash, not as removable media.
Android Studio open failed: EROFS (Read-only file system) when creating a File
FileOutputStream fos = new FileOutputStream("tours");
This is not pointing anywhere that you can read or write. Always use a method to get at the base locations where you can read and write.
If you are looking to write to internal storage, change this to be something like:
FileOutputStream fos = new FileOutputStream(new File(getFilesDir(), "tours"));
If you are looking to write to external storage, change this to be something like:
FileOutputStream fos = new FileOutputStream(new File(getExternalFilesDir(null), "tours"));
Exception open failed: EROFS (Read-only file system) thrown by
One scenario that can explain this is a change in android:sharedUserId
.
android:sharedUserId
is an attribute you can put on <manifest>
. If 2+ apps have the same sharedUserId
and are signed by the same signing key, they can read/write each others files on internal storage.
However, if you change the sharedUserId
value for a shipping app — including adding one after having shipped originally without it — existing users will be locked out of their internal storage files. Android switches the Linux uid that the app runs as, but it does not change the ownership of the files. As a result, the app no longer has rights to its files. This is the exception that I would expect to see in this case.
It is this sort of problem that caused Google to finally deprecate android:sharedUserId
in Android Q, with a plan to formally discontinue it in a future release.
In your case, I am guessing that you did not set or change android:sharedUserId
.
That is the only scenario that I know of where internal storage for your app would be reported as read-only, where you as the developer caused the problem.
Everything else that I can think of is outside of your control:
Perhaps the user rooted their device and accidentally changed file ownership or file permissions on your files
Perhaps the user is a script kiddie who is reverse-engineering your app and is fussing around with
android:sharedUserId
Perhaps the user has been hit with ransomware, and part of its attack was to change permissions on app files via some security flaw
java.io.FileNotFoundException: open failed: EROFS (Read-only file system)
... saves to an external directory
This is not quite right. You are trying to save it in the root system partition which is always read-only.
From javadoc of Environment.getRootDirectory():
Return root of the "system" partition holding the core Android OS. Always present and mounted read-only.
Solution : just save your file somewhere else :
StreamResult result = new StreamResult(new File(android.os.Environment.getExternalStorageDirectory(), "upload_data.xml"));
Note that :
- you must ask the permission to write to the external storage :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- External storage is not always available. The partition may not be mounted -yet- when you try to write on it. (just be careful)
Related Topics
How to Use Getsystemservice in a Non-Activity Class (Locationmanager)
Android Maps API V2 with Custom Markers
How to Always Run a Service in the Background
What Is the Class R in Android
Android Volley - How to Isolate Requests in Another Class
How to Debug Android Application Line by Line Using Eclipse
How to Turn Speaker On/Off Programmatically in Android 4.0
Problem to Load Flv Video in Webview
Android Fade in and Fade Out with Imageview
Ontaskremoved() Not Getting Called in Huawei and Xiaomi Devices
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
Android Http Connection Exception
Android Spanned, Spannedstring, Spannable, Spannablestring and Charsequence
Android Studio, Suddenly Got Gpu Driver Issue When Running Emulator