Search code examples
androidandroid-11android-storageprivacy-policy

Ask for advice of adopting to new Android storage privacy policy


My app has a backup/restore feature which export data to a file either to a file in device, or share to DropBox, email etc. When restore user can select that file to import data back. But after the new Android storage privacy policy:

https://developer.android.com/about/versions/11/privacy/storage

My app can no longer see the files that exported if the app that restore is after uninstall and reinstall, or in a new device which must be a different installation. It seems all the app can see are the files is generate. This breaks the purpose of the backup/restore feature.

Can anyone advice how do my app should change in order to help user keep data across different installation? Thank you for any suggestions or advice.


Solution

  • The new policy is about reading/writing every file in the device, you can still ask for the system for external files that will be delivered to you as a Uri that you just open and read

        val intentGetPDF = Intent(Intent.ACTION_GET_CONTENT)
        intentPDF.type = fileType
        intentPDF.addCategory(Intent.CATEGORY_OPENABLE)
        ctx.startActivityForResult(Intent.createChooser(intentFetPDF, ""), REQUEST_PDF)
    

    And from onActivityResult

    val iS = ctx.contentResolver.openInputStream(data.data as Uri)
    

    To save the files you now need to share it from your internal app storage trough a Uri from FileProvider

        val fileUri = FileProvider.getUriForFile(ctx, "apppackage.provider", "your internal file path here")
        val intent = Intent(MediaStore.ACTION_SEND)
        intent.putExtra(Intent.EXTRA_STREAM, fileUri)
        startActivity(intent)
    

    You will need to setup a FileProvider in the AndroidManifest so it work