Problem with persistent tree permissions in Android

Dear All,

I am currently at a loss as to how to solve a problem with persistent tree permissions in Android.

Why does the Android SAF tree sharing not work in the following code?

Android (SDK 36.1) returns the following single tree permission, which is persistent.

DBG(AndroidDocumentPermission::getPersistedPermissions()[0].getUrl().toString(true));

// URL: content://com.android.externalstorage.documents/tree/primary%3ADocuments%2FappFolder%2FappFolder_1.0

The permission queries return true

AndroidDocumentPermission::getPersistedPermissions()[0].isReadPermission()

AndroidDocumentPermission::getPersistedPermissions()[0].isWritePermission()

I believe that this means that files in the shared folder have r/w permissions and can therefore be read or written without a native FileChooser.

However, the AndroidDocument ‘testAdDoc’, which was created with the URL ‘test’ triggers all DBG outputs during queries. The file is physically located in the folder.

URL test = URL("content://com.android.externalstorage.documents/document/primary%3ADocuments%2FappFolder%2FappFolder_1.0%2Fconfig.tcp");

auto testAdDoc = AndroidDocument::fromDocument(test);

if(!testAdDoc.getInfo().canRead())
    DBG("testAdDoc: NO READ Permission");

if(!testAdDoc.getInfo().canWrite())
    DBG("testAdDoc: NO WRITE Permission");

if(!testAdDoc.hasValue())
    DBG("testAdDoc.hasValue FAILED");

std::unique_ptr<InputStream> inputStream (testAdDoc.createInputStream());
if (inputStream == nullptr)
{
	DBG("testAdDoc: Failed to open input stream");
}

Here are both URLs again:

content://com.android.externalstorage.documents/tree/primary%3ADocuments%2FappFolder%2FappFolder_1.0

content://com.android.externalstorage.documents/document/primary%3ADocuments%2FappFolder%2FappFolder_1.0%2Fconfig.tcp

I would be very grateful for any advice on how to solve this problem.

UPDATE:

using testAdDoc = AndroidDocument::fromFile() works!

URL testURL = URL("content://com.android.externalstorage.documents/document/primary%3ADocuments%2Fxavur%2Fxavur_1.0%2Fconfig.tcp");

auto testAdDoc = AndroidDocument::fromDocument(testURL);

if(!testAdDoc.hasValue())
{
    DBG("testAdDoc.hasValue FAILED");

==> testAdDoc = AndroidDocument::fromFile(testURL.getLocalFile()); // <===========

    if(testAdDoc.hasValue())
    {
        DBG("true, with r/w rights and testAdDoc.getInfo().exists()"); // !!!!
    }
}

std::unique_ptr<InputStream> inputStream (testAdDoc.createInputStream());
if (inputStream == nullptr)
{
	DBG("testAdDoc: Failed to open input stream");
}

So far so good, but testAdDoc.createInputStream() still returns nullptr. :thinking: