用户第一次打开我的应用程序时,我将一些数据写入本地文件。
我知道我的应用程序需要权限才能写入此文件,所以我正在使用 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/