title: How to write into a file picked by user in Android 12
body: <p>I encounter a problem only on Android 12, I try to add some lines into a file localized in a folder that the user pick.
  I worked on a Samsung A70 (Android 11), and to accomplish that I launch an <code>Intent.ACTION_OPEN_DOCUMENT_TREE</code>, check all to have to be checked (not null, file existing etc).</p>
  <p>After that, i launch an <code>getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);</code> to keep the permission.</p>
  <hr />
  <p>And this is working on the Samsung A70, but I need this in my Samsung S22 (Android 12) too, but it doesn't work.</p>
  <p>I got an <code>java.io.FileNotFoundException: Failed to open for writing: java.io.FileNotFoundException: open failed: EACCES (Permission denied)</code> when trying to write something.</p>
  <h2>What i try:</h2>
  <p>I decide to change to an <code>Intent.ACTION_OPEN_DOCUMENT</code> to pick only the file i need, take a persistant permission too, but again this error.</p>
  <p>I try to print all files access into the folder that the user picked:</p>
  <pre><code>DocumentFile dir = DocumentFile.fromTreeUri(this, data.getData());
              
  if(null != dir){
     for(DocumentFile d : dir.listFiles()){
          System.out.println(d.getName() + &quot;&gt; Access: (R/W) &quot; + d.canRead() + &quot; - &quot; + d.canWrite());
     }
  }
  </code></pre>
  <p>And I got this with Android 12:<a href="https://i.stack.imgur.com/qVl4S.png" rel="nofollow noreferrer">S22 logcat of the function above</a></p>
  <p>And this on Android 11: <a href="https://i.stack.imgur.com/FxXqP.png" rel="nofollow noreferrer">A70 logcat of the function above</a></p>
  <p>With the same code.</p>
  <hr />
  <p>The file I need to write on is the json file (hardware[...].json)</p>
  <hr />
  <p>In the Android Doc I saw that the canWrite methods can return true, even though my app cannot write into, so they recommended to query the flag <code>FLAG_SUPPORTS_WRITE</code> directly, what i done:</p>
  <pre><code>try{
        System.out.println(data.getFlags());
        System.out.println(data.getFlags() &amp; DocumentsContract.Document.FLAG_SUPPORTS_WRITE);
        ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(data.getData(), &quot;w&quot;); //Error here
        FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
  
        fos.close();
  }catch//***//
  </code></pre>
  <p>And got this into the logcat (Android 12): <a href="https://i.stack.imgur.com/8dLWJ.png" rel="nofollow noreferrer">logcat test flags android 12</a></p>
  <p>And on Android 11: <a href="https://i.stack.imgur.com/PkMJD.png" rel="nofollow noreferrer">logcat test flags android 11</a></p>
  <p>The first value is 0b1000011, the <code>FLAG_SUPPORTS_WRITE</code> value is 0b10 (1 &lt;&lt; 1) (<a href="https://developer.android.com/reference/android/provider/DocumentsContract.Document#FLAG_SUPPORTS_WRITE" rel="nofollow noreferrer">Doc</a>)
  So the second print is 2, showing that the flag is ok. But the line after, an error is throwed.</p>
  <hr />
  <p>That all, if anyone have an idea to solve this problem with me, thank you! :D</p>