deepak raj
deepak raj

Reputation: 3801

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

Heard Android Q introduced a new security feature called “Scoped Storage” which restricts access files in external storage. My problem is I have to save a text document to a user specified location from the App. Is this requires any kind of permission rather than READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in Q devices?

Upvotes: 1

Views: 389

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007399

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.

Upvotes: 1

Related Questions