只读文件系统 [英] Read-only file system

查看:125
本文介绍了只读文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将png文件存储在手机的内存中,但是遇到了一些困难.

I'm trying to store a png file in the internal memory of the phone, but I am having some difficulties.

我正在使用以下代码:

private void storeImage(Bitmap image, String nombre) {
    try {
        File file = new File(name + ".png");
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }  
}

但是此LogCat消息失败:

But it fails with this LogCat message:

访问文件时出错:打开失败:EROFS(只读文件系统)

Error accessing file: open failed: EROFS (Read-only file system)

我不知道如何更改文件的权限,或者是否有更好的方法将png文件存储在手机中.

I don't know how to change the permissions of the file or if there are better ways to store a png file in the phone.

谢谢!

推荐答案

由于不建议使用kitkat,因此推荐使用

since kitkat the use of Environment.getExternalStorageDirectory(); is discouraged, in favor of Context.getExternalFilesDir which returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media. This directory and its content will be deleted upon uninstall of the app and you won't the WRITE_EXTERNAL_STORAGE use permission to use the returned directory.

您试图在/内部编写的代码是只读的.如果要将位图存储在SD卡中,则必须进行以下更改:

The code you have is trying to write inside / which is read only. If you want to store your bitmap inside the SD card, you have to change:

File file = new File(nombre+".png");

进入:

File root = Environment.getExternalStorageDirectory();
File file = new File(root, nombre+".png");

这两行将尝试将nombre+".png"写入sdcard的根目录,从而以某种方式污染后者.该文件将保留在那里,直到您手动删除它为止,并且手机上安装的所有应用程序都可以访问该文件.在PC上安装sdcard或使用文件浏览器也可以访问它.

These two lines will attempt to write nombre+".png" into the root of your sdcard, polluting somehow the latter. The file will remain there untill you delete it manually and will be accessible to the all the apps installed on your phone. Mounting the sdcard on your pc or using a file browser will give you access to it as well.

这些行:

final FileOutputStream fos = openFileOutput(nombre+".png", Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();

在内部存储(/data/data/you.app.name)的应用程序专用空间中写入文件.由于它是私有的,只有您的应用程序才能访问它.唯一的例外是如果您拥有一部已打过电话的手机.

write the file in the app private space in internal storage (/data/data/you.app.name). Since its private only your app will be able to access it. The only exception is if you have a rooted phone.

要检索它,您必须使用:

to retrieve it, you have to use:

final FileInputStream fis = openFileOutput(nombre+".png", Context.MODE_PRIVATE);

这篇关于只读文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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