java.io.IOException: Read-only file system

强颜欢笑 提交于 2019-12-19 08:27:04

问题


File mediaDir = new File("media");
if (!mediaDir.exists()){
    mediaDir.createNewFile();
    mediaDir.mkdir();

}

File f = new File("/data/data/com.test.image/files/media/Voucher.jpg");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(b);
fos.close();

Try to write a file in phone internal memory but it throw an error: java.io.IOException: Read-only file system


回答1:


Guess /data/data is not external storage. You need to have root permission in order to write to the /data directory. Refer Data directory has no read/write permission in Android




回答2:


You are creating file in Android system's root directory for which no application is allowed to.

To create directory specific to your application, use getDir(String dirName, int mode) instead of new File("media"). By calling only, you can create media directory and use it.

No need to do so many stuffs what you are doing in your above code. Also no need to give any permission in manifest too.




回答3:


Use mediaDir.mkdirs(); instead of mediaDir.mkdir(); also you must require WRITE_EXTERNAL_STORAGE permission




回答4:


You can try something like the following to make a folder in an Activity

this.getDir("folder_name", Context.MODE_PRIVATE);

source



来源:https://stackoverflow.com/questions/9372805/java-io-ioexception-read-only-file-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!