尝试在Android中创建文件:打开失败:EROFS(只读文件系统) [英] Trying to create a file in Android: open failed: EROFS (Read-only file system)

查看:554
本文介绍了尝试在Android中创建文件:打开失败:EROFS(只读文件系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行:

final FileOutputStream outputStream = new FileOutputStream(name);

导致 FileNotFoundException ,其中的消息是 / 2ozjfFQzwv:打开失败:EROFS(只读文件系统)其中2ozjfFQzwv是我作为文件名称传递的。

results in a FileNotFoundException with the message being /2ozjfFQzwv: open failed: EROFS (Read-only file system) where "2ozjfFQzwv" is what I passed as the name of the file.

我已经尝试过这个与 WRITE_INTERNAL_STORAGE 的权限。如何创建这个文件来写?

I have tried this with and without the WRITE_INTERNAL_STORAGE permission. How do I create this file for writing?

或者,我只是希望能够给一个新的活动的图像,它是太大,无法序列化它一个额外的。有没有比将文件写入文件更方便,再次读取呢?所有这些问题似乎都是写给一张SD卡,我不想做,因为很多人没有SD卡插槽。

Alternatively, I just want to be able to give an image to a new activity, and it is too large to serialize it in an extra. Is there an easier way than writing it to a file then reading from it again? All the questions on here seem to be about writing to an SD card, which I don't want to do because many people don't have SD card slots.

推荐答案


我已尝试使用和不使用WRITE_INTERNAL_STORAGE权限。

I have tried this with and without the WRITE_INTERNAL_STORAGE permission.

在Android中没有 WRITE_INTERNAL_STORAGE 权限。

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.

请使用 FileOutputStream 构造函数,它采用文件对象。根据您可以写入的某个位置创建文件对象,例如:

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()(在您的活动或其他上下文
  • getExternalFilesDir()(在您的活动其他上下文

  • getFilesDir() (called on your Activity or other Context)
  • getExternalFilesDir() (called on your Activity or other Context)

后者将需要 WRITE_EXTERNAL_STORAGE 作为一个权限。

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.


因为很多人没有SD卡插槽

because many people don't have SD card slots

SD卡插槽是无关紧要的。 99%的Android设备用户将拥有外部存储空间 - 例外情况是4岁以上的用户删除SD卡的设备。自2010年年中制造的设备将外部存储器作为板载闪存的一部分,而不是可移动介质。

"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中创建文件:打开失败:EROFS(只读文件系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆