5

I have a question regarding storage changes in Android 11. If I understand correctly, app can read/write/delete the files it created. Also I can then access those files for example like this:

File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/ExampleApp/" + fileName);

ExampleApp is the app folder I created in the public Downloads folder. But once the app gets uninstalled, it losses the ownership over all the files it created. Is there a way to regain the ownership of the ExampleApp folder by the app once it gets reinstalled? One could use scoped storage & ACTION_OPEN_DOCUMENT_TREE, but that would then have to be done on every fresh start of application, no? Files in the app folder are in multiple sub-folders and of non media format, so I can't utilize Media Store API.

Permissions used:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Scoped storage permissions:
https://developer.android.com/training/data-storage#scoped-storage

tomazj
  • 303
  • 3
  • 13
  • 1
    Use ACTION_OPEN_DOCUMENT_TREE to let the user select your folder. You only have to do that once if you take persistable uri permission. You can let open the picker gui in your directory. – blackapps Aug 17 '21 at 11:39
  • There is no way other than using MediaStore Api and also ACTION_OPEN_DOCUMENT_TREE doesn't work on Downloads folder as per the documentation. – Vaibhav Goyal Aug 17 '21 at 12:02
  • 1
    @Vaibhav Goyal, users can pick subdirs of Download folder. And OP is using a subdir. `let the user select your folder`. – blackapps Aug 17 '21 at 12:53

1 Answers1

2

Is there a way to regain the ownership of the ExampleApp folder by the app once it gets reinstalled?

No.

One could use scoped storage & ACTION_OPEN_DOCUMENT_TREE, but that would then have to be done on every fresh start of application, no?

No. Programmers can use takePersistableUriPermission() on ContentResolver to get durable access to user-selected tree.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • With PersistableUri I can't use FIle class to access my files anymore right? Everything would have to be rewritten using DocumentFile. – tomazj Aug 18 '21 at 06:44
  • 2
    @tomazj: "With PersistableUri I can't use FIle class to access my files anymore right?" -- correct. – CommonsWare Aug 18 '21 at 10:52