1

I noticed that my program on Android Q can not create any folder or save and open a file.
I've read Google's documentation but I did not find the answer or at least I did not understand Because there is a very small sample code, and as I figured out, every time you create a file, you need to open a special window
Someone can tell me how can I get a full access for special folder in sdcard? I do not want to open a different window every time and confirm it

Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28
hadi khodabandeh
  • 585
  • 2
  • 7
  • 20

1 Answers1

3

Long term, you have no means of forcing the user to give you access to a test/ directory on external storage.

You have three main options.

You can use getExternalFilesDir(), getExternalCacheDir(), or getExternalMediaDir(). These are methods on Context, and they each return a File pointing to a directory on external storage for which you have full read-write filesystem access. From the user's standpoint, these will be under Android/data/.../, where ... is your application ID.

Or, you can use ACTION_OPEN_DOCUMENT_TREE and the Storage Access Framework. Using startActivityForResult() with an ACTION_OPEN_DOCUMENT_TREE Intent will give you a Uri that points to a document tree, which may be on external storage, removable storage, or in the cloud. You can call takePersistableUriPermission() on a ContentResolver to get long-term access to that tree, and you can use DocumentFile.fromTreeUri() to work with that tree. However, this is not filesystem access — it is a bit closer to working with a Web server.

Or, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This will give you legacy filesystem behavior on Android Q devices. This is scheduled to be removed for Android R, so this is not a suitable long-term solution.

I blogged about the Android Q storage changes a lot this year; here is my last(?) post on it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491