oreo android中的java.io.FileNotFoundException:(权限被拒绝)
以下代码用于在SD卡(外部存储器)中创建的文件中写入联系人详细信息: 其中,cpath是我的文件路径,它位于sd卡中 在这里,我在网上看到了错误:oreo android中的java.io.FileNotFoundException:(权限被拒绝),android,android-permissions,android-storage,Android,Android Permissions,Android Storage,以下代码用于在SD卡(外部存储器)中创建的文件中写入联系人详细信息: 其中,cpath是我的文件路径,它位于sd卡中 在这里,我在网上看到了错误: FileOutputStream mFileOutputStream = new FileOutputStream( cpath, true); 我得到下面的日志:- java.io.FileNotFoundException: /storage/9016-4EF8/AllBackupDemo
FileOutputStream mFileOutputStream = new FileOutputStream(
cpath, true);
我得到下面的日志:-
java.io.FileNotFoundException: /storage/9016-4EF8/AllBackupDemo/cont20181031050512.vcf (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:287)
at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
at java.io.FileOutputStream.<init>(FileOutputStream.java:142)
注意:
解决方案: 我分析并比较了我的代码与您的代码,发现您使用的所有代码都是相同的 必须更改获取错误的行。此错误是由于使用了
FileOutputStream
,这表示您是从文件系统而不是从DocumentTree
进行访问
因此,不是:
FileOutputStream mFileOutputStream = new FileOutputStream(
cpath, true);
试着这样做:
try {
OutputStream out = getContentResolver().openOutputStream(cpath.getUri());
try {
// Do whatever you wish to do.
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException("Something went wrong : " + e.getMessage(), e);
}
我不确定您的程序的实际流程,您可能希望自己完成。但是,这肯定会给你一个继续下去的想法
希望有帮助。在选择SAF时,您在哪里选择了SD卡?在创建文件之前,我正在检查,如果需要,请选择“使用无文档意图”。是的,您必须在文档树中选择。。并在整个应用程序周期内维护该对象@Ümañgßürmån我更新了我的代码。检查这里,我已经选择了使用文档树。你不应该使用android N及以上版本的FileProvider吗。检查是否
uritreeuri=data.getData()代码>返回非空。
FileOutputStream mFileOutputStream = new FileOutputStream(
cpath, true);
try {
OutputStream out = getContentResolver().openOutputStream(cpath.getUri());
try {
// Do whatever you wish to do.
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException("Something went wrong : " + e.getMessage(), e);
}