1

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.

  • "The device camera seems to put photos into a public directory on the SDCard so it can be done" -- the "device camera" is a pre-installed app. Device manufacturers and custom ROM developers can give pre-installed apps rights that ordinary apps lack. "I built a solution using Storage Access Framework but it is very very slow" -- then perhaps ask a separate Stack Overflow question, with a [mcve] showing your implementation, along with the details of your performance analysis (method tracing, etc.), and ask for assistance in improving its performance. – CommonsWare Feb 04 '19 at 13:21
  • Note: This is not a duplicate of [stackoverflow keep-files-after-uninstallation-of-android-app](https://stackoverflow.com/questions/9726990/keep-files-after-uninstallation-of-android-app) because the accepted answer there is not valid any more because of [deperecated in android-q](https://stackoverflow.com/questions/56468539/getexternalstoragepublicdirectory-deprecated-in-android-q) . Hopefully [commonsware's blog](https://commonsware.com/blog/2019/04/22/death-external-storage-more-story.html) keeps us up to date – k3b Jul 17 '19 at 12:14

0 Answers0