title: Android SD Card Write Permission using SAF (Storage Access Framework)
body: <p>After a lot of findings about how to write(and rename) a file in SD Card (android 5 and above), I think the new SAF provided by android will be required to take permission from user to write SD card file. </p>
  
  <p>I have seen in this File Manger Application <strong>ES File Explorer</strong> that initially it takes read and write permission the following way as shown in pictures.</p>
  
  <p><a href="https://i.stack.imgur.com/ZNDgX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZNDgX.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/WYSI9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/WYSI9.png" alt="Picture 2"></a></p>
  
  <p>After selecting sd card, the write permission is granted.</p>
  
  <p>So in the same way I tried to use SAF, but failed in renaming a file. My code:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      rename = (Button) findViewById(R.id.rename);
  
      startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), 42);
  }
  
  @Override
  public void onActivityResult(int requestCode,int resultCode,Intent resultData) {
      if (resultCode != RESULT_OK)
          return;
      Uri treeUri = resultData.getData();
      DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
      grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  }
  
  public void renameclick(View v) {
      File ff = new File("/storage/sdcard1/try1.jpg");
      try {
          ff.createNewFile();
      } catch (Exception e) {
          Log.d("error", "creating");
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Still after running the code I get EAacces permission denied.</p>