android kotlin java.io.FileNotFoundException: /storage/emulated/0/number.txt: open failed: EACCES (Permission denied)

后端 未结 1 627
栀梦
栀梦 2020-11-30 13:07

I\'m writing an app for Android 10 using kotlin. The app has to read file named number.txt from internal storage.

But it always fails to do so:

         


        
相关标签:
1条回答
  • 2020-11-30 14:07

    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.

    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.

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

    • 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.

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

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

    0 讨论(0)
提交回复
热议问题