java.io.FileNotFoundException: /storage/emulated/0/Download/xxx: open failed: EACCES (Permission den

 

问题

java.io.FileNotFoundException: /storage/emulated/0/Download/xxx: open failed: EACCES (Permission denied)

首先需要说明一下我的这个问题是Android 10才出现的,Android10以下的都没有,这里主要说的不是动态申请访问文件的权限问题。因为我已经动态申请了权限,并且在获得权限后存储文件报的这个错误。

解决

先说一下解决版本只需要在AndroidManifest.xml文件的application 标签下 加一条属性 android:requestLegacyExternalStorage="true"就可以解决了。

原因

究其原因就是Android10弃用了管理分区外部储存

 

官方文档

 

所以如果要在Android10上创建文件的话需要如下所示的代码创建文件。
上边的解决办法是禁用了这个管理分区

禁用分区

 

但是将来是要适配Android10的 所以最终的解决办法是如下代码所示的方式创建文件。

// Here are some examples of how you might call this method.
// The first parameter is the MIME type, and the second parameter is the name
// of the file you are creating:
//
// createFile("text/plain", "foobar.txt");
// createFile("image/png", "mypicture.png");

// Unique request code.
private static final int WRITE_REQUEST_CODE = 43;
...
private void createFile(String mimeType, String fileName) {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    // Filter to only show results that can be "opened", such as
    // a file (as opposed to a list of contacts or timezones).
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Create a file with the requested MIME type.
    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_TITLE, fileName);
    startActivityForResult(intent, WRITE_REQUEST_CODE);
}

 

 

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值