How can I ask for external file access permission in Android Q?

后端 未结 1 1806
孤街浪徒
孤街浪徒 2021-01-24 10:42

Heard Android Q introduced a new security feature called “Scoped Storage” which restricts access files in external storage. My problem is I hav

相关标签:
1条回答
  • 2021-01-24 11:26

    My problem is I have to save a text document to a user specified location from the App

    Use ACTION_CREATE_DOCUMENT to allow the user to specify the location. You can use use a ContentResolver and its openOutputStream() method to get an OutputStream for the Uri that you get back from ACTION_CREATE_DOCUMENT. You can then write your text to that OutputStream.

    Is this requires any kind of permission rather than READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in Q devices?

    ACTION_CREATE_DOCUMENT requires no permissions and works back to Android 4.4.

    Alternatively, you can write your file to a directory identified by getExternalFilesDir() on Context. This portion of external storage is visible to the user on Android Q and requires no additional permissions.

    Or, if you keep your targetSdkVersion to 28 or lower, you can use WRITE_EXTERNAL_STORAGE and write where you want, as you did on Android 9.0+. However, bear in mind that this approach will stop working with Android R and when you raise your targetSdkVersion past 28 (e.g., to comply with 2020's Play Store requirements). So, using ACTION_CREATE_DOCUMENT, or perhaps getExternalFilesDir(), is the better long-term answer.

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