takePersistableUriPermission Access lost after documentfile deleted

284 views Asked by At

After picking a folder with

registerForActivityResult( new ActivityResultContracts.OpenDocumentTree ...

I'm taking writing permission on it

getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

Later in the app, I have written files to the user selected dir

saveDir = DocumentFile.fromTreeUri(App.getInstance(), Uri.parse(uriStringSavedInPrefs));

The problem is: when I create a file its created successfully

DocumentFile finalResFile =saveDir.createFile("image/*",resFileName);

But then I delete it later and it gets deleted too

DocumentFile.fromTreeUri(App.getInstance(),finalResFile .getUri()).delete();

Bute later I try to create a new file again and I get an error

DocumentFile finalResFile =saveDir.createFile("image/*",someNewFileName);

Failed query: java.lang.SecurityException: Permission Denial: reading com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/tree/primary%3A.AndroidData/document/primary%3A.AndroidData/children from pid=32273, uid=10546 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
 Failed to create document

Why is the write access getting lost after deleting a single file on it?

1

There are 1 answers

0
Omkar T On

If I delete documentfile like this,I lose access,still don't know why (I'm not deleting the user picked directory,just a file I created inside it)

DocumentFile.fromTreeUri(App.getInstance(),uri).delete();

Using this to delete file somehow works (I dont lose access write access)

DocumentsContract.deleteDocument(App.getInstance().getContentResolver(),uri);
                                                

I hate SAF