使用特定权限在 Android 中创建文件
Create a file in Android with certain permission
我正在开发一个 Android 应用程序,我必须实现一个函数来创建一个包含不同文件的文件夹。
我希望文件夹中的文件被隐藏,这一点不是问题,但我想为文件分配一定的权限,例如我需要文件不能被用户读取/写入,但只能从应用程序中读取/写入。
另外,如果应用程序被卸载,我希望文件夹中的文件也被删除。
这是我用来创建隐藏文件夹的代码:
File JSONStorage = new File(Environment.getExternalStorageDirectory(), ".BMA");
if (!JSONStorage.exists()) {
if (!JSONStorage.mkdirs()) {
Log.wtf("log: ", "Failed to create directory");
}
}
来自官方 Android 文档:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
这将在设备内存中创建文件。该文件只能由应用程序读取。 (Context.MODE_PRIVATE
)
我正在开发一个 Android 应用程序,我必须实现一个函数来创建一个包含不同文件的文件夹。
我希望文件夹中的文件被隐藏,这一点不是问题,但我想为文件分配一定的权限,例如我需要文件不能被用户读取/写入,但只能从应用程序中读取/写入。
另外,如果应用程序被卸载,我希望文件夹中的文件也被删除。
这是我用来创建隐藏文件夹的代码:
File JSONStorage = new File(Environment.getExternalStorageDirectory(), ".BMA");
if (!JSONStorage.exists()) {
if (!JSONStorage.mkdirs()) {
Log.wtf("log: ", "Failed to create directory");
}
}
来自官方 Android 文档:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
这将在设备内存中创建文件。该文件只能由应用程序读取。 (Context.MODE_PRIVATE
)