title: Unable to write to external storage; Access is denied
body: <p>I have a class which stores images downloaded from Facebook when the app is started for the first time. What I do is that I would use a class to create a folder (if it does not exist before) and store all the images there.</p>
  
  <p><strong>Important</strong> I've had success on several devices of different API (all below 23). Files are written and retrieved successfully. Lately I have a new phone which uses 6.0 (API 23) and somehow the code to write files into storage system no longer works. I can no longer create the folder in the sdcard and that leads to the error.</p>
  
  <p>As an added information, the phone is a rooted nexus 5X, not a external SD card. The app runs on API 23 and dangerous permission including storage is properly authorized before the app can even be used.</p>
  
  <p>Using the tried and true method of getExternalStorageDirectory, which returns emulated/0/. The Environment.MEDIA_MOUNTED indicates that access is granted to the storage. <strong>The problem is the moment I try to create a folder it tells me i cannot do so.</strong></p>
  
  <p><strong>.canwrite indicates that the mkdirs directory I am trying to create is not accessible</strong></p>
  
  <p>For your reference, my manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/&gt;
  </code></pre>
  
  <p>My method that writes to the external storage:</p>
  
  <pre><code>public static String storeImage(Bitmap bitmap, String filename) {
      String stored = null;
      String folder_main = "Samepage";
      String root = Environment.getExternalStorageDirectory().toString();
      String state = Environment.getExternalStorageState();
      Log.i(TAG,"Storage state is " + state);
      File folder = new File(root + "/Samepage");
      Log.i(TAG, folder.toString());
  
      if (!Environment.MEDIA_MOUNTED.equals(state)) {
          Log.e(TAG,"Storage is not mounted!");
      }
  
      if (folder.canWrite()) {
          Log.i(TAG, "External storage is accessible");
  
          if (!folder.exists()) {
              folder.mkdirs();
  
          }
      } else 
          Log.e(TAG, "Default storage is not accessible");
  
  
      File sdcard = Environment.getExternalStorageDirectory();
      if (folder.exists()) {
          File file = new File(root, filename + ".png");
          if (file.exists())
              file.delete();
  
          try {
              FileOutputStream out = new FileOutputStream(file);
              bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
              out.flush();
              out.close();
              stored = "success";
              Log.i(TAG, "User image created successfully");
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      return stored;
  }
  </code></pre>