简体   繁体   English

只读文件系统

[英]Read-only file system

I'm trying to store a png file in the internal memory of the phone, but I am having some difficulties. 我正在尝试将png文件存储在手机的内部存储器中,但是遇到了一些困难。

I'm using this code: 我正在使用此代码:

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());
    }  
}

But it fails with this LogCat message: 但是此LogCat消息失败:

Error accessing file: open failed: EROFS (Read-only file system) 访问文件时出错:打开失败:EROFS(只读文件系统)

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. 我不知道如何更改文件的权限,或者是否有更好的方法将png文件存储在手机中。

Thanks! 谢谢!

Edit: since kitkat the use of Environment.getExternalStorageDirectory(); 编辑:自kitkat使用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. 不建议使用Context.getExternalFilesDir ,该方法返回应用程序可以放置其拥有的永久文件的主共享/外部存储设备上目录的绝对路径。 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. 此目录及其内容将在卸载应用程序后删除,并且您将没有WRITE_EXTERNAL_STORAGE使用权限才能使用返回的目录。

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: 如果要将位图存储在SD卡中,则必须进行以下更改:

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

into: 变成:

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

These two lines will attempt to write nombre+".png" into the root of your sdcard, polluting somehow the latter. 这两行将尝试将nombre+".png"写入sdcard的根目录,从而以某种方式污染后者。 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. 在PC上安装sdcard或使用文件浏览器也可以访问它。

These lines: 这些行:

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

write the file in the app private space in internal storage ( /data/data/you.app.name ). 将文件写入内部存储( /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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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