abs*_*ent 5 file-io android 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();
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)
为什么会发生这种情况?我可以解决它吗?一个直接的碰撞是这样使用应用程式时不愉快的初次体验。
Tof*_*mad -2
在 android 清单中授予权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
确保您提供正确的带有扩展名的文件路径
java.io.FileNotFoundException
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4226 次 |
| 最近记录: |