Up until Android Pie I always stored files which the app needed to get stored on /sdcard/my-app/
, which I got via
File fBaseDir = new File(Environ
In Android Q direct File access is disabled by default for the apps outside their private folders. Here few strategies you can use in your case:
requestLegacyExternalStorage
to have the old behavior but it won't work anymore with Android R, so it's really a short term solution;getExternalFilesDir()
method. It's your private folder, other apps could access these files only if they have READ_EXTERNAL_STORAGE
permission. In this case it would be good to use FileProvider
to grant access to other apps to your files.getPrimaryStorageVolume().createOpenDocumentTreeIntent()
of class StorageManager
to ask for access to the extenal primary volume. In this case you need user consent and you won't be able to use File
api directly anyway, but using DocumentFile
class you have a very similar interface, so this is the solution closer to the old behavior. It works if you need to perform operations in foreground and background, i.e. without user interaction with the exception the first interaction to request the permission.I link Flipper library for point 3, it helps to manage files like in older android versions.