且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android kotlin java.io.FileNotFoundException:/storage/emulated/0/number.txt:打开失败:EACCES(权限被拒绝)

更新时间:2022-03-28 22:39:08

对于Android 10,您可以将android:requestLegacyExternalStorage="true"添加到清单中的<application>元素中.这使您可以选择旧式存储模型,并且您的外部存储代码应该可以正常工作.

For Android 10, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your external storage code should work.

对于读操作,您应该仍然可以照着Android 11+上的原始路径"进行操作.即使您将targetSdkVersion提高到30或更高,也可以进行更改.

And for read operations, you should be able to still do what you are doing on Android 11+, courtesy of the "raw paths" change, even after you raise your targetSdkVersion to 30 or higher.

但是,对于 write 操作,您必须在2021年下半年之前切换到其他位置:

However, for write operations, you have until the second half of 2021 to switch to something else:

  • 使用Context上的方法(例如getExternalFilesDir())访问应用程序可以写入的外部存储上的目录.您不需要任何权限即可在Android 4.4+上使用这些目录.但是,卸载应用程序后,存储在其中的数据将被删除.

  • Use methods on Context, such as getExternalFilesDir(), to get at directories on external storage into which your app can write. You do not need any permissions to use those directories on Android 4.4+. However, the data that you store there gets removed when your app is uninstalled.

使用存储访问框架,例如ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENT.

Use the Storage Access Framework, such as ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT.

如果您的内容是媒体,则可以使用MediaStore将媒体放置在标准媒体位置.

If your content is media, you can use MediaStore to place the media in standard media locations.