title: Android SAF: Uri of a folder (on disk) from DocumentsContract.getTreeDocumentId has different form, in respect to the previously authorized one
body: <p>I am testing the Storage Access Framework for my app.</p>
  
  <p>In my app the user selects a folder as an access point to the filesystem for the app itself.</p>
  
  <p>This method is used:</p>
  
  <pre><code>static public void openPickerForFolderSelection(Activity activity, int requestCode)
  {
      Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
      intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); //useful?
      activity.startActivityForResult(intent, requestCode);
  }
  </code></pre>
  
  <p>In the onActivityResult method the following code is called:</p>
  
  <pre><code>static public String takePermanentReadWritePermissions(Activity activity, Intent data)
  {
  int takeFlags = data.getFlags()
          &amp;
          (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();
  
  }
  </code></pre>
  
  <p>Example, the Uri is:</p>
  
  <pre><code>content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Faccesspoint1
  </code></pre>
  
  <p>In fact, the Uri happens to be authorized as detected by this method:</p>
  
  <pre><code>static boolean arePermissionsGranted(Activity activity, String uriString)
  {
      ContentResolver resolver = activity.getContentResolver();
      List&lt;UriPermission&gt; list = resolver.getPersistedUriPermissions();
      for (int i = 0; i &lt; list.size(); i++){
      Log.d("compare",uriString+" "+Uri.decode(list.get(i).getUri().toString())+" "+String.valueOf(list.get(i).isWritePermission())+" "+String.valueOf(list.get(i).isReadPermission()));
      if(((Uri.decode(list.get(i).getUri().toString())).equals( uriString)) &amp;&amp; list.get(i).isWritePermission()&amp;&amp; list.get(i).isReadPermission()){
              return true;
          }
  </code></pre>
  
  <p>then, when it is necessary to create a new folder in the selected folder the following method is called but there is an error before attempting to create the folder:</p>
  
  <pre><code>static public boolean createFolderInFolder(Activity activity,String parentFolderUriString,String folderName)
  {
  boolean result=false;
  ContentResolver contentResolver;
  Uri parentFolderUri=null;
  String id=DocumentsContract.getTreeDocumentId(Uri.parse(parentFolderUriString));
  Log.d("id ",id); 
  parentFolderUri= DocumentsContract.buildTreeDocumentUri(PROVIDER_AUTHORITY,id);
  try {
  //error here
      DocumentsContract.createDocument(contentResolver,parentFolderUri,DocumentsContract.Document.MIME_TYPE_DIR,folderName);
              result=true;
  } catch (FileNotFoundException e) {
      result =false;
  }
  return result;
  }
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{1426e0c 13943:com.example.app/u0a439} (pid=13943, uid=10439) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
  </code></pre>
  
  <p>(note that using </p>
  
  <pre><code>parentFolderUri=Uri.parse(parentFolderUriString); 
  </code></pre>
  
  <p>directly leads to an "invalid Uri" error)</p>
  
  <p>The fact is that the permissions are granted on a different Uri than the one from DocumentsContract.getTreeDocumentId(Uri.parse(parentFolderUriString));</p>
  
  <p>Here's the log:</p>
  
  <pre><code>D/compare: content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Faccesspoint1 content://com.android.providers.downloads.documents/tree/raw:/storage/emulated/0/Download/accesspoint1 true true
  </code></pre>
  
  <p>Indeed the id value is:</p>
  
  <pre><code>raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Faccesspoint1
  </code></pre>
  
  <p>that leads to the final Uri value</p>
  
  <pre><code>content://com.google.android.apps.docs.storage/tree/raw%253A%252Fstorage%252Femulated%252F0%252FDownload%252Faccesspoint1
  </code></pre>
  
  <p>So it seems that I should request permissions also on this different version of the Uri, if I want that no security exception (with permission denial) is issued? But it seems not possible to take the permissions on a different form uri.</p>
  
  <p>How to solve this?</p>