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

前端 未结 8 2216
臣服心动
臣服心动 2020-11-27 05:52

This line:

final FileOutputStream outputStream = new FileOutputStream(name);

results in a FileNotFoundException with the messa

相关标签:
8条回答
  • 2020-11-27 06:10

    Here is simple sample from android developer.

    Basically, you can write a file in the internal storage like this :

    String FILENAME = "hello_file";
    String string = "hello world!";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
    
    0 讨论(0)
  • 2020-11-27 06:12

    try using the permission of WRITE_EXTERNAL_STORAGE You should use that whether there is an external card or not.

    This works well for me:

    path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MOVIES);
    File file = new File(path, "/" + fname);
    

    and places my files in the appropriate folder

    0 讨论(0)
  • 2020-11-27 06:14

    Adding

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

    in manifest and using same as Martin:

    path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); 
    File file = new File(path, "/" + fname);
    

    It worked.

    0 讨论(0)
  • 2020-11-27 06:18

    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 your Activity or other Context)
    • getExternalFilesDir() (called on your Activity or other Context)

    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.

    0 讨论(0)
  • 2020-11-27 06:21

    If anyone getting this in unit/instrumentation testing, make sure you call getFilesDir() on the app context, not the test context. i.e. use:

    Context appContext = getInstrumentation().getTargetContext().getApplicationContext();
    

    not

    Context appContext = InstrumentationRegistry.getContext;
    
    0 讨论(0)
  • 2020-11-27 06:22

    As others have mentioned, app on Android can't write a file to any folder the internal storage but their own private storage (which is under /data/data/PACKAGE_NAME ).

    You should use the API to get the correct path that is allowed for your app.

    read this .

    0 讨论(0)
提交回复
热议问题