java.io.FileNotFoundException: /storage/emulated/0/Download/xxx.xxx: open

问题

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);
}

你可能感兴趣的:(java.io.FileNotFoundException: /storage/emulated/0/Download/xxx.xxx: open)