且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

只读文件系统

更新时间:2022-12-16 18:16:40

由于不建议使用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);