小编abs*_*ent的帖子

写入 context.getFilesDir() 时“权限被拒绝”

用户第一次打开我的应用程序时,我将一些数据写入本地文件。

我知道我的应用程序需要写入这个文件的权限,所以我使用 getFilesDir() 像这样:

File ins = new File(context.getFilesDir(), "INSTALLATION");
// do some things
FileOutputStream out = FileOutputstream.new(ins);
out.write(data.toString().getBytes());
out.close();
Run Code Online (Sandbox Code Playgroud)

这适用于 99.9% 的情况。但是我看到一小部分例外情况,表明我无权创建此文件。例如:

java.lang.RuntimeException: Unable to create application com.blah.blah.Blah
java.lang.RuntimeException: java.io.FileNotFoundException: /INSTALLATION (Permission denied)
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?我可以解决它吗?一个直接的碰撞是这样使用应用程式时不愉快的初次体验。

file-io android permission-denied

5
推荐指数
1
解决办法
4226
查看次数

标签 统计

android ×1

file-io ×1

permission-denied ×1