title: Trying to takePersistableUriPermission() fails for custom DocumentsProvider via ACTION_OPEN_DOCUMENT
body: <p>I am trying to write a custom <code>DocumentsProvider</code> that allows other apps to take persistable permissions to the Uris it provides</p>
  
  <p>I have a <code>DocumentsProvider</code> that I declare in my <code>AndroidManufest.xml</code> as follows</p>
  
  <pre><code>&lt;provider
     android:name="com.cgogolin.myapp.MyContentProvider"
     android:authorities="com.cgogolin.myapp.MyContentProvider"
     android:grantUriPermissions="true"
     android:exported="true"
     android:permission="android.permission.MANAGE_DOCUMENTS"
     android:enabled="@bool/atLeastKitKat"&gt;
    &lt;intent-filter&gt;
      &lt;action android:name="android.content.action.DOCUMENTS_PROVIDER" /&gt;
    &lt;/intent-filter&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p>and my app has the <code>MANAGE_DOCUMENTS</code> permission set</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /&gt;
  </code></pre>
  
  <p>(apparently this is not necessary but adding/removing it also doesn't matter).
  I can then see my provider when I open the <code>ACTION_OPEN_DOCUMENT</code> picker UI with</p>
  
  <pre><code>Intent openDocumentIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  openDocumentIntent.addCategory(Intent.CATEGORY_OPENABLE);
  openDocumentIntent.setType("application/pdf");
  openDocumentIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
  startActivityForResult(openDocumentIntent, EDIT_REQUEST);
  </code></pre>
  
  <p>and, after picking a file from my provider there, in the <code>onActivityResult()</code> method of my App I can then successfully open the file provided by my <code>DocumentsProvider</code> via the <code>Uri</code> I get from <code>intent.getData()</code>.</p>
  
  <p>However, trying to persist read or write permissions with</p>
  
  <pre><code>getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  </code></pre>
  
  <p>always fails with an exception like</p>
  
  <pre><code>No permission grant found for UID 10210 and Uri content://com.cgogolin.myapp.MyContentProvider/document/tshjhczf.pdf
  </code></pre>
  
  <p>If I pick a file from the google drive or downloads provider in the picker UI taking permissions in this way works. So I think the problem is in my provider.</p>
  
  <p>Why is there no permission grant created despite me specifying <code>android:grantUriPermissions="true"</code>?</p>
  
  <p>How can I convince Android to create such a permission grant for me?</p>
  
  <p>After all I don't think I can do it myself, as I cannot know the <code>UID</code> of the process that opened the picker UI, or at least not that I knew how.</p>