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

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

问题描述

这一行:

final FileOutputStream outputStream = new FileOutputStream(name);

导致 FileNotFoundException 消息为 /2ozjfFQzwv: open failed: EROFS (Read-only file system) 其中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?

如果您的应用以超级用户权限运行,则您不会,除非在有 root 权限的设备上运行.您正在尝试写入应用无权访问的内部存储的根目录.

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.

请使用带有 File 对象的 FileOutputStream 构造函数的版本.根据您可以写入的某个位置创建该 File 对象,例如:

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()(在您的 Activity 或其他 Context 上调用)
  • getExternalFilesDir()(在您的 Activity 或其他 Context 上调用)
  • 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 设备用户将拥有外部存储设备——用户移除 SD 卡的 4 年以上旧设备除外.自 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天全站免登陆