title: Android 6.0 Marshmallow. Cannot write to SD Card
body: <p>I have an app that uses external storage to store photographs. As required, in its manifest, the following permissions are requested</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>and it uses the following to retrieve the required directory</p>
  
  <pre><code>File sdDir = Environment
              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  
  SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd", Locale.US);
  String date = dateFormat.format(new Date());
  storageDir = new File(sdDir, getResources().getString(
              R.string.storagedir)
              + "-" + date);
  
  // Create directory, error handling
  if (!storageDir.exists() &amp;&amp; !storageDir.mkdirs()) {
   ... fails here
  </code></pre>
  
  <p>The app works fine on Android 5.1 to 2.3; it has been on Google Play for over a year.</p>
  
  <p>Following an upgrade of one of my testing phones (Android One) to 6, it's now returning an error when trying to create the requisite directory, "/sdcard/Pictures/myapp-yy-mm".</p>
  
  <p>The sd card is configured as "Portable storage". I've formatted the sd card. I've replaced it. I've rebooted. All to no avail.</p>
  
  <p>Also, the built-in android screenshot functionality (via Power+Lower volume) is failing "due to limited storage space, or it isn't allowed by the app or your organisation".</p>
  
  <p>Any ideas?</p>