Storage Access Provider - Creation of folder in au

2019-08-30 08:44发布

My app lets the user create a folder with the SAF picker and then wants to create a folder in it.

This is how the picker is called:

static public void openPickerForFolderCreation(Activity activity,int requestCode)
{

    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.setType("vnd.android.document/directory");
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    activity.startActivityForResult(intent, requestCode);
}

In the onActivityResult method the following code is called:

static public String takePermanentReadWritePermissions(Activity activity, Intent data)
{
    int takeFlags = data.getFlags()
            &
            (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    ContentResolver resolver = activity.getContentResolver();
    resolver.takePersistableUriPermission(data.getData(),takeFlags);
    return data.getData().toString();

}

This is how the permission are verified (and it happens that they are in my case):

static boolean arePermissionsGranted(Activity activity, String uriString)
{
    ContentResolver resolver = activity.getContentResolver();
    List<UriPermission> list = resolver.getPersistedUriPermissions();
    for (int i = 0; i < list.size(); i++){
        if(((Uri.decode(list.get(i).getUri().toString())).equals( uriString)) && list.get(i).isWritePermission()&& list.get(i).isReadPermission()){
            return true;
        }
    }
    return false;
}

Then the app tries to create a folder in the authorized directory (folder) with:

static public boolean creatFolderInFolder(Activity activity,String parentFolderUriString,String folderName)
{
boolean result=false;
ContentResolver contentResolver;
contentResolver = activity.getContentResolver();
Uri parentFolderUri=null;
parentFolderUri= DocumentsContract.buildDocumentUri(PROVIDER_AUTHORITY,
        DocumentsContract.getDocumentId(Uri.parse(parentFolderUriString)));
try {
    DocumentsContract.createDocument(contentResolver,parentFolderUri,DocumentsContract.Document.MIME_TYPE_DIR,folderName);
            result=true;
} catch (FileNotFoundException e) {
    result =false;
}
return result;
}
}

But if the parent folder was created on disk, I get:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.app/com.mydomain.app.activityName}: java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{f114a14 18915:com.mydomain.app/u0a412} (pid=18915, uid=10412) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

while if the parent folder was created on the GDrive cloud space (accessible from the picker) I get:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.app/com.mydomain.app.activityName}: java.lang.SecurityException: Permission Denial: writing com.google.android.apps.docs.storagebackend.StorageBackendContentProvider uri content://com.google.android.apps.docs.storage/document/acc%3Drandomcharactershere from pid=20197, uid=10413 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

They are not exceptions, the error is issued by this line:

DocumentsContract.createDocument(contentResolver,parentFolderUri,DocumentsContract.Document.MIME_TYPE_DIR,folderName);

Is SAF working correctly? It seems that I did everything right in my app code, so why I get those errors?

0条回答
登录 后发表回答