android - 写入 context.getFilesDir() 时为 "Permission denied"

标签 android file-io permission-denied

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

我知道我的应用程序需要权限才能写入此文件,所以我正在使用 getFilesDir() ,如下所示:

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

这在 99.9% 的情况下都有效。但是我看到一小部分异常说我没有创建这个文件的权限。例如:

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

为什么会这样?我可以解决它吗?立即崩溃是这样应用程序的不愉快的第一次体验。

最佳答案

在android list 中授予权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

确保在获取文件时提供正确的扩展名文件路径

java.io.FileNotFoundException

关于android - 写入 context.getFilesDir() 时为 "Permission denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274180/

相关文章:

java - 使用 Java 中的多线程删除多个文件

c - fgets 没有得到最后一个字符

c - 如何将文本文件中的该表输入到 C 中?

node.js - Node 中的 Jest 安装出现错误 gyp ERR!堆栈错误 : EACCES: permission denied, rmdir 'build'

bash - .bashrc : Permission denied

android - 如何修复错误 :Could not find com. google.gms : google-services: 3. 0.0.?

java - 找不到 Google 健身类(class)

android - 为什么 Android 渐变闪屏仅在 5.0 上导致立即重启?

ubuntu - 安装和设置 Phalcon 框架后,禁止访问我的本地主机和 Kubuntu 上的子文件夹

Android DatePicker 无法识别何时通过键入更改了年份