简体   繁体   中英

Android SD card on emulator writing fails with IOException EROFS (Read-only filesystem), have correct permission

My android app can't write a file to the AVD's SD card, here's the code:

    File root = Environment.getExternalStorageDirectory();

    Log.w("sdcard", "root.canWrite() = " + root.canWrite());

    try {           
        File myFile = new File(root + File.pathSeparator + "mysdfile.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append("testing testing 123");
        myOutWriter.close();
        fOut.close();
    } catch (IOException e) {
        Log.w("ExternalStorage", "Error writing", e);
    }

root.canWrite() returns true, then this exception is raised:

java.io.IOException: open failed: EROFS (Read-only file system)

The manifest file has the WRITE_EXTERNAL_STORAGE permission (direct child of manifest):

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

I've also tried remounting the SD card in the AVD shell using:

mount -o remount rw /mnt/sdcard

...but to no avail. The AVD also has the "SD card support" flag set. Not sure what to do from here, any ideas?

Starting from Honeycomb, Android devices by default do not allow third-party apps from writing to external removable storage. You can check this by firing up your adb shell, go to /system/etc/permissions/platform.xml, and look for this line:

<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
    <group gid="sdcard_rw" />
</permission>

Third party apps are not part of "sdcard_rw" group, instead they're part of "media_rw". If your phone is rooted (your emulator should be), you can modify the platform.xml file to add another group that has this permission. It should look like this:

<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
    <group gid="sdcard_rw" />
    <group gid="media_rw" />
</permission>

Some manufacturers (I know Samsung does) that still have SD Card slots on their phones fixed this themselves, but not even Nexus 7 or GNex has this fixed by default.

To anyone interested, the problem was that I had made an sdcard.iso image and had eclipse passing that to the AVD as the -sdcard command line argument. For whatever reason, the image was read-only. Fixed the problem by clearing the command line args and editing the AVD to start with a new sd card of size 512mb, rather than a file.

In my case i have mistake with the slash. while specifying "Additional Emulator Command Line Options" in eclipse

Actually it should be -sdcard C:/Kamal/sdcard/emulator_sdcard

and i was putting -sdcard C:\\Kamal\\sdcard\\emulator_sdcard

Then after restarting the Eclipse & Emulator it works.

try {

  InputStream inputStream2 = openFileInput("database.sqlite3"); File outFile =new File(Environment.getExternalStorageDirectory(), "database15.sqlite3"); outFile.createNewFile(); OutputStream out= new FileOutputStream(outFile); byte[] buf = new byte[1024]; int len; while((len = inputStream2.read(buf))> 0) { out.write(buf); } out.close(); Toast.makeText(getBaseContext(),getFilesDir().toString()+"Done", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException ex) { Toast.makeText(getBaseContext(),"file not found", Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_LONG).show(); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM