title: Write to external storage on Android 7
body: <p>I want to write to the external storage in my Android app, but I don't seem to get the permissions right. The App runs on Android 7, so I request the permissions on runtime like this:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  
  if (permission != PackageManager.PERMISSION_GRANTED) {
      // We don't have permission so prompt the user
      int ACCESS_EXTERNAL_STORAGE_STATE = 1;
      ActivityCompat.requestPermissions(this,
              new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
              ACCESS_EXTERNAL_STORAGE_STATE);
  }
  
  permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
  
  if (permission != PackageManager.PERMISSION_GRANTED) {
      // We don't have permission so prompt the user
      int ACCESS_EXTERNAL_STORAGE_STATE = 1;
      ActivityCompat.requestPermissions(this,
              new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
              ACCESS_EXTERNAL_STORAGE_STATE);
  }
  ...
      @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          switch (requestCode)
          {
              case 1: {
                  if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED)
                  {
                      //reload my activity with permission granted or use the features what required the permission
                  } else
                  {
                      Toast.makeText(this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
                  }
              }
          }
      }
  </code></pre>
  
  <p>One observation I made is that onRequestPermissionsResult seems to return that the permission was not granted, but I was also never prompted when starting the app to permit writing to external storage.</p>
  
  <p>I set these permissions in the Manifest:</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;application&gt;
  </code></pre>
  
  <p>The code to write to the storage is here:</p>
  
  <pre><code>public static void appendByteBuffer(byte[] data)
  {
      String filename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.raw";
      //filename value is "/storage/emulated/0/test.raw"
      File rawAudioFile = new File(filename);
      if (!rawAudioFile.exists())
      {
          try
          {
              rawAudioFile.createNewFile();
          }
          catch (IOException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      try (FileOutputStream output = new FileOutputStream(filename, true)) {
          output.write(data);
      } catch (IOException e) {}
  }
  </code></pre>
  
  <blockquote>
    <p>03-16 14:50:49.988 19145-19306/? W/System.err: java.io.IOException:
    Permission denied 03-16 14:50:49.988 19145-19306/? W/System.err:<br>
    at java.io.UnixFileSystem.createFileExclusively0(Native Method) 03-16
    14:50:49.988 19145-19306/? W/System.err:     at
    java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
    03-16 14:50:49.989 19145-19306/? W/System.err:     at
    java.io.File.createNewFile(File.java:948) 03-16 14:50:49.989
    19145-19306/? W/System.err:     at
    com.meeter.study.AppUtils.appendByteBuffer(AppUtils.java:76) 03-16
    14:50:49.989 19145-19306/? W/System.err:     at</p>
  </blockquote>