I need to be able to read and write data on a removable SDCard such that it will not be deleted if the app is uninstalled. A lot of data!
I have an app that works with more than 50,000 files, 10 GBytes (mostly images and videos). This data is initially loaded directly onto the SDCard using an SD Adapter in a PC and then regularly updated (small numbers of files) through FTP on the Android device. (There are a small group of users in different countries.)
This all works very well if I use getExternalFilesDirs() and Environment.isExternalStorageRemovable(file) to put the data on the External SDCard. Example path:- /storage/1234-5678/Android/data.com.me.myapp/files/MyDataDirectory
However, sometimes the user needs to uninstall and reinstall the app. In this case all the data is destroyed and it takes several hours to rebuild if the SDCard is attached to a PC and several days if it has to be rebuilt through FTP.
I can put all the files into a top level directory on the SDCard and read them. The app works fine and the files won't be deleted on uninstall. But the FTP update fails with Permission Denied on any attemot to write to the SDCard..
The app uses ActivityCompat.requestPermissions(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 101) to get permission from the user.
ContextCompat.checkSelfPermission(act, permission.WRITE_EXTERNAL_STORAGE) returns PackageManager.PERMISSION_GRANTED after this has been done.
I would be quite happy to write my data into a subdirectory of a Public directory, such as ... File externalPublicDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES);
However:- Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) always returns a path to internal storage. Not a removable SDCard. My data is too big for this to be acceptable.
This all used to work perfectly up to KitKat, using a top level directory on the SDCard, but progressive security controls have blocked the easy solutions. I am now using Nougat on all the devices that run the app.
The device camera seems to put photos into a public directory on the SDCard so it can be done.
I built a solution using Storage Access Framework but it is very very slow.
Can anyone suggest a way to Write to a Public directory on the SDCard using the file system?
Thanks.