title: Android Storage Access Framework: Which path should I use to save a file?
body: <p>I get permissions to my removable sd-card via SAF with code below.
  With a click on the Floating action button "File" I give the permissions to my app. After that I want to create a file with the FAB "folder".</p>
  
  <p>When I try to save the file to the uri-path it says that the path does not exist.
  Which path do I have to use?
  (String[] path only for testing different paths...)</p>
  
  <p><strong>FAB-File</strong></p>
  
  <pre><code>FloatingActionButton fab_new_file = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_file);
  fab_new_file.setOnClickListener(new View.OnClickListener() {
  
      @Override
      public void onClick(View view) {
          System.out.print("FAB File clicked");
          Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
          intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
          intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
          intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
          intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
          startActivityForResult(intent, 42);
      }
  });
  mainPath = this.getArguments().getString("mainpath");
  getFilesFromDir(mainPath, mainPath);
  return rLayoutFrgEmpresas;
  </code></pre>
  
  <p><strong>onActivityActionResult</strong></p>
  
  <pre><code>@Override
      public void onActivityResult(int requestCode, int resultCode, Intent data){
          System.out.print("onActivityResult started");
          if (resultCode == Activity.RESULT_OK) {
              if (data != null) {
                  treeUri = data.getData();
  
                  System.out.print("treeUri: "+treeUri);
                  if (requestCode == 42){
  
                      int takeFlags = data.getFlags();
                      takeFlags &amp;= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  
                      getContext().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
                      System.out.print("Permissions: "+getContext().getContentResolver().getPersistedUriPermissions());
  
                      editor = getActivity().getSharedPreferences("spicysoftware.com.phonemanager", MODE_PRIVATE).edit();
                      editor.putString("treeUri", treeUri.toString());
                      if(editor.commit()){
                          System.out.print("treeUri sharedPref added");
                      }
                  }
              }
          }
          super.onActivityResult(requestCode, resultCode, data);
      }
  </code></pre>
  
  <p><strong>FAB-Folder</strong></p>
  
  <pre><code>FloatingActionButton fab_new_folder = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_folder);
  fab_new_folder.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          Log.i("Information: ", "FAB Folder clicked");
  
          String str_treeUri = prefs.getString("treeUri", null);
          Uri myUri = Uri.parse(str_treeUri);
  
          Log.i("Information: ", "myUri from shared Pref: "+myUri);
  
          String[] path = {
                  ""+myUri,
                  ""+myUri+"/",
                  "/com.android.externalstorage.documents/tree/B9BE-18A6/",
                  "/tree/B9BE-18A6/",
                  "/storage/B9BE-18A6/"
          };
  
          for(int i=0; i&lt;path.length; i++) {
              File testfile = new File("" + path[i] + "testfile" + System.currentTimeMillis() + ".txt");
              if (!testfile.exists()) {
                  Log.i("Information: ", testfile+ " does not exist");
                  try {
                      testfile.createNewFile();
                      getFilesFromDir(textView_currentPath.getText() + "", textView_currentPath.getText() + "");
                      Log.i("File " + testfile, "has been created!");
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  });
  </code></pre>
  
  <p><strong>LogCat</strong></p>
  
  <pre><code>*Before clicked FAB-File*
  04-12 16:41:26.748 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
  04-12 16:41:26.821 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
  04-12 16:41:26.833 2197-2197/spicysoftware.com.phonemanager I/Information:: FAB File clicked
  04-12 16:41:26.868 2197-2197/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
  04-12 16:41:27.036 2197-2197/spicysoftware.com.phonemanager D/InputTransport: Input channel destroyed: fd=73
  04-12 16:41:34.131 2197-2197/spicysoftware.com.phonemanager I/Information:: onActivityResult started
  04-12 16:41:34.140 2197-2197/spicysoftware.com.phonemanager I/Information:: Permissions: [UriPermission {uri=content://com.android.externalstorage.documents/tree/B9BE-18A6%3A, modeFlags=3, persistedTime=1492008094137}]
  04-12 16:41:34.151 2197-2197/spicysoftware.com.phonemanager I/Information:: treeUri sharedPref added
  04-12 16:41:34.173 2197-2285/spicysoftware.com.phonemanager D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
  *After clicked FAB-File*
  
      *Before clicked FAB-Folder*
  04-12 17:04:36.011 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
  04-12 17:04:36.084 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
  04-12 17:04:36.093 25159-25159/spicysoftware.com.phonemanager I/Information:: FAB Folder clicked
  04-12 17:04:36.093 25159-25159/spicysoftware.com.phonemanager I/Information:: myUri from shared Pref: content://com.android.externalstorage.documents/tree/B9BE-18A6%3A
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager I/Information:: content:/com.android.externalstorage.documents/tree/B9BE-18A6%3Atestfile1492009476094.txt does not exist
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-12 17:04:36.094 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager I/Information:: content:/com.android.externalstorage.documents/tree/B9BE-18A6%3A/testfile1492009476095.txt does not exist
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  04-12 17:04:36.095 25159-25159/spicysoftware.com.phonemanager I/Information:: /com.android.externalstorage.documents/tree/B9BE-18A6/testfile1492009476095.txt does not exist
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  04-12 17:04:36.096 25159-25159/spicysoftware.com.phonemanager I/Information:: /tree/B9BE-18A6/testfile1492009476096.txt does not exist
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: No such file or directory
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  04-12 17:04:36.097 25159-25159/spicysoftware.com.phonemanager I/Information:: /storage/B9BE-18A6/testfile1492009476097.txt does not exist
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err: java.io.IOException: Permission denied
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$1.onClick(StorageFragment.java:172)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-12 17:04:36.098 25159-25159/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  04-12 17:04:48.021 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processKey 0
  04-12 17:04:48.046 25159-25159/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processKey 1
      *After clicked FAB-Folder*
  </code></pre>