title: Can't write to external storage unless app is restarted after granting permission
body: <p>App unable to write to external storage on Android 6.0 (I'm testing on emulator), even after WRITE_EXTERNAL_STORAGE has been granted at runtime; unless the app is killed and restarted.</p>
  
  <p>Snippet from AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>build.gradle</p>
  
  <pre><code>android {
  compileSdkVersion 23
  buildToolsVersion "23.0.1"
  
  defaultConfig {
      ......
      minSdkVersion 15
      targetSdkVersion 23
  }
  </code></pre>
  
  <p>Whenever I need to write to external storage (for backup) I check whether or not I have permission.</p>
  
  <pre><code>            if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp;
                      getActivity().getBaseContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                  requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_RW_EXTERNAL_STORAGE);
                  mPendingAction = PendingAction.Backup;
              } else {
                  BackupRestoreService.startBackup(getActivity().getBaseContext());
              }
  </code></pre>
  
  <p>I also have the following</p>
  
  <pre><code>    @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          Log.d("SettingsActivity", "grantResultsLength: " + grantResults.length);
          if (requestCode == PERMISSION_REQUEST_RW_EXTERNAL_STORAGE) {
              Log.d("SettingsActivity", "grantResultsLength: " + grantResults.length);
              if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  switch (mPendingAction) {
                      case Backup:
                          BackupRestoreService.startBackup(getActivity().getBaseContext());
                          mPendingAction = PendingAction.None;
                          break;
                      case Restore:
                          break;
                      default:
                  }
  
              } else {
                  Toast.makeText(getActivity(),
                          "Permission denied",
                          Toast.LENGTH_SHORT).show();
              }
          }
      }
  </code></pre>
  
  <p>When the permission is granted by user, the following code</p>
  
  <pre><code>File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), DIR_MY_PORTFOLIO);
  if (!file.mkdirs())
          Log.d("Backup", "Unable to create directories");
  final String outputFilename = new SimpleDateFormat("'Backup'-yyyyMMdd-hhmmss'.mpb'", Locale.US).format(new Date());
  File outputFile = new File(getBackupStorageDir(), outputFilename);
  Log.d("Backup", "Can write to file: " + outputFile.canWrite());
  Log.d("Backup", "File exists: " + outputFile.exists());
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>    in.whoopee.myportfolio D/Backup: Unable to create directories
      in.whoopee.myportfolio D/Backup: Can write to file: false
      in.whoopee.myportfolio D/Backup: File exists: false
      in.whoopee.myportfolio W/System.err: java.io.FileNotFoundException: /storage/09FD-2F0C/Download/My Portfolio/Backup-20151011-051318.mpb: open failed: EACCES (Permission denied)
  </code></pre>
  
  <p>If, after the permission is granted, the app is killed and restarted, everything goes perfect and backup file is created in external storage.</p>
  
  <p>Please suggest what I am doing wrong.</p>