title: Requesting and allowing WRITE_EXTERNAL_STORAGE permission at runtime has no effects on the current session
body: <p>this is regarding the new model of <a href="https://developer.android.com/training/permissions/requesting.html">runtime permissions introduced in Android Marshmallow</a> when requesting <code>Manifest.permission.WRITE_EXTERNAL_STORAGE</code> permission.</p>
  
  <p>In short, what I am experiencing is that if I request (and the user allows) <code>Manifest.permission.WRITE_EXTERNAL_STORAGE</code> permission, the app won't be able to read and write from the external storage directory <em>until I destroy and restart the app</em>.</p>
  
  <p>This is what I am doing/experiencing:</p>
  
  <p>My app starts from a state where:</p>
  
  <pre><code>ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
  </code></pre>
  
  <p>This is, I don't have permissions to to access external storage.</p>
  
  <p>Then, I request permission to Manifest.permission.WRITE_EXTERNAL_STORAGE just as Google explains</p>
  
  <pre><code>private void requestWriteExternalStoragePermission() {
      // Should we show an explanation?
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          new AlertDialog.Builder(this)
                  .setTitle("Inform and request")
                  .setMessage("You need to enable permissions, bla bla bla")
                  .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          ActivityCompat.requestPermissions(MendeleyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_PERMISSION_WRITE_EXTERNAL_STORAGE);
                      }
                  })
                  .show();
      } else {
          ActivityCompat.requestPermissions(MendeleyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_PERMISSION_WRITE_EXTERNAL_STORAGE);
      }
  }
  </code></pre>
  
  <p>Once the user allows the permission, <code>onRequestPermissionsResult</code> gets invoked. </p>
  
  <pre><code>@Override
  public void onRequestPermissionsResult(int requestCode,  String permissions[], int[] grantResults) {
      switch (requestCode) {
          case RC_PERMISSION_WRITE_EXTERNAL_STORAGE: {
              // If request is cancelled, the result arrays are empty.
              if (grantResults.length &gt; 0 &amp;&amp; PackageManager.PERMISSION_GRANTED
                  // allowed 
              } else {
                  // denied
              }
              break;
          }
      }
  }
  </code></pre>
  
  <p>The <code>allowed</code> block is executed, confirming the user has granted permissions.</p>
  
  <p><strong>Immediately</strong> after this, if I don't destroy and open the app again, I still have no access permission to external storage. More specifically:</p>
  
  <pre><code>hasWriteExternalStoragePermission();                  // returns true
  Environment.getExternalStorageDirectory().canRead();  // RETURNS FALSE!!
  Environment.getExternalStorageDirectory().canWrite(); // RETURNS FALSE!!
  </code></pre>
  
  <p>So, it seems the Android runtime thinks I have permissions, but the file system doesn't...
  Indeed, trying to access <code>Environment.getExternalStorageDirectory()</code> throws the exception:</p>
  
  <pre><code>android.system.ErrnoException: open failed: EACCES (Permission denied)
  at libcore.io.Posix.open(Native Method)
  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
  at libcore.io.IoBridge.open(IoBridge.java:438)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:87) 
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:72) 
  </code></pre>
  
  <p>If I now <strong>destroy the app and open it again</strong>, the behaviour becomes as it should, being able to read and write in the external storage folder. </p>
  
  <p>Is anyone experiencing this?</p>
  
  <p>I am using one official emulator with:</p>
  
  <ul>
  <li>Latest Android 6.0 (API 23) API 23, Rev 1.</li>
  <li>Emulator running Intel x86 Atom System Image, API 23, Rev 1.</li>
  </ul>
  
  <p>I build the app with:</p>
  
  <pre><code>android {
      compileSdkVersion 23
      buildToolsVersion "22.0.1"
  
      defaultConfig {
          minSdkVersion 16
          targetSdkVersion 23
      }
  ...
  }
  </code></pre>
  
  <p>If someone confirms this and I am not the only one I guess we'll need to open a bug, but I hope I am doing something wrong, as I think such a core feature is unlikely to be buggy in the SDK.</p>