“许可被拒绝”写入 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();
这在 99.9% 的情况下有效。但我看到一小部分异常,表明我没有创建此文件的权限。例如:
java.lang.RuntimeException: Unable to create application com.blah.blah.Blah
java.lang.RuntimeException: java.io.FileNotFoundException: /INSTALLATION (Permission denied)
为什么会发生这种情况?我可以解决这个问题吗?立即崩溃对于应用程序来说是一次不愉快的初次体验。
The first time a user opens my app, I write a little bit of data to a local file.
I understand that my app needs permission to write to this file, so I'm using getFilesDir() like so:
File ins = new File(context.getFilesDir(), "INSTALLATION");
// do some things
FileOutputStream out = FileOutputstream.new(ins);
out.write(data.toString().getBytes());
out.close();
This works in 99.9% of cases. But I see a small trickle of exceptions saying that I don't have permission to create this file. For example:
java.lang.RuntimeException: Unable to create application com.blah.blah.Blah
java.lang.RuntimeException: java.io.FileNotFoundException: /INSTALLATION (Permission denied)
Why does this happen? Can I work around it? An immediate crash is such an unpleasant first experience with an app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 android 清单中授予权限,
确保您提供正确的带有扩展名的文件路径,因为您正在获得
Give permission in android manifest
make sure your giving correct path of file with extension as you are getting