title: How to save files with File API to a user-selected location in Android 11?
body: <p>I am very confused with <code>Scoped Storage</code> and <code>Storage Access Framework</code> (SAF) and all restrictions.. I need to use <code>File API</code>, not <code>Uri</code>, so:<br />
  Android API level &lt;= 29 (Android 10) save to <code>/storage/emulated/0</code> works (with <code>requestLegacyExternalStorage</code>).<br />
  Android API level = 30 (Android 11) save to <code>/storage/emulated/0</code> does not work (<code>IOException: Operation not permitted</code>).</p>
  <p><code>Environment.getExternalStorageDirectory()</code> is deprecated. OK, so <code>getExternalFilesDirs()</code> In Android 11:</p>
  <pre><code>ctx.getExternalFilesDirs(): /storage/emulated/0/Android/data/&lt;package name&gt;/files //External storage in internal memory
  ctx.getExternalFilesDirs(): /storage/16F4-3A1C/Android/data/&lt;package name&gt;/files //SDcard
  </code></pre>
  <p>works, because it's app's storage. But users want to have app's folder in root of external storage <code>/storage/emulated/0/MySpecificFolder</code> or sdcard <code>/storage/16F4-3A1C/MySpecificFolder</code>. I am working with <code>File API</code> which should be functional again (unlike Android10).</p>
  <p>How can I write to user specific directory, for example to <code>/storage/emulated/0/MySpecificFolder</code>?</p>
  <p>I tried to use:</p>
  <pre><code>Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
  startActivityForResult(intent, PICK_FOLDER);
  </code></pre>
  <p>which open File manager. User creates new directory (because root can't use): <code>/storage/emulated/0/MySpecificFolder</code>. Next I am trying create a new file via:</p>
  <pre><code>File nomedia = new File(&quot;/storage/emulated/0/MySpecificFolder&quot;, &quot;.nomedia&quot;);
  if (!nomedia.exists()) {
      nomedia.createNewFile();
  }
  </code></pre>
  <p>but it's not working <code>IOException: Operation not permitted</code>. There is probably problem with write permission.</p>
  <p>This line works:</p>
  <pre><code>DocumentFile newFile = pickedDir.createFile(&quot;text/plain&quot;, &quot;My Novel&quot;);
  </code></pre>
  <p>which works with <code>Uri</code>. <strong>Why is it not possible to use <code>File API</code>?</strong> When I call <code>createFile()</code> which uses <code>Uri</code>, that's working, but when I call <code>createNewFile()</code> which uses <code>File API</code>, that's not working. So user can select own directory, but only when app working with <code>Uri</code>?</p>
  <p>How to get <code>path</code> (for later use with <code>File API</code>) from SAF, not only <code>Uri</code>.</p>
  <pre><code>// Get Uri from Storage Access Framework.
  Uri treeUri = data.getData();
  DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
  Log.i(TAG, &quot;Tree uri: &quot;+treeUri.getPath());  //returns /tree/primary:My
  Log.i(TAG, &quot;Picked dir: &quot;+pickedDir.getUri().getPath()); //returns /tree/primary:My/document/primary:My
  </code></pre>
  <p>I need use <code>File API</code>, because app. creates video+photo+data files which need to be together.</p>
  <p>Question: Is it possible for user to choose own path + create folder (via SAF probably) and return <code>File</code> (file descriptor) of new created folder by user to app?</p>