1

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?

deepak raj
  • 3,331
  • 1
  • 12
  • 20

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you help me with https://stackoverflow.com/questions/63543414/rename-file-of-the-external-storage-which-is-created-by-app-in-android-10-worki – jazzbpn Aug 24 '20 at 03:47