title: mkdir() works while inside internal flash storage, but not SD card?
body: <p>I am currently building a file management app that allows the user to browse the file system of their device. The user starts off in the root directory <code>/</code> of their device, but can browse to any location they want such as the internal flash storage or SD card.</p>
  
  <p>One of the critical requirements of this app is to allow the user to create new folders anywhere. A feature like this would be immensely useful for the app. However, the <a href="http://developer.android.com/reference/java/io/File.html#mkdir()"><code>File#mkdir()</code></a> method does not work at all in the SD card directory.</p>
  
  <p>I added the appropriate permissions to the manifest file. I also wrote a test to see which directories (all of which exist on my Lollipop 5.0 device) allow the creation of a new folder. From my observations, <a href="http://developer.android.com/reference/java/io/File.html#mkdir()"><code>File#mkdir()</code></a> only works when inside the internal flash storage directory.</p>
  
  <p><strong>Note:</strong> please don't confuse <a href="http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()"><code>Environment#getExternalStorageDirectory()</code></a> with the SD card location, as explained by <a href="https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html">this article</a>. Also on Lollipop 5.0, I believe <code>/storage/emulated/0/</code> and <code>/storage/sdcard0/</code> refer to the internal flash storage while <code>/storage/emulated/1/</code> and <code>/storage/sdcard1/</code> refer to the SD card (which is at least true for the device I am testing with).</p>
  
  <p>How can I create new files and folders in areas outside the external storage path on non-rooted Android devices?</p>
  
  <hr>
  
  <p><strong>Manifest:</strong></p>
  
  <pre><code>...
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  ...
  </code></pre>
  
  <p><strong>Test:</strong></p>
  
  <pre><code>...
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final String NEW_FOLDER_NAME = "TestFolder";
          testPath(new File(Environment.getExternalStorageDirectory(), NEW_FOLDER_NAME));
          testPath(new File("/storage/emulated/0/", NEW_FOLDER_NAME));
          testPath(new File("/storage/emulated/1/", NEW_FOLDER_NAME));
          testPath(new File("/storage/sdcard0/Download/", NEW_FOLDER_NAME));
          testPath(new File("/storage/sdcard1/Pictures/", NEW_FOLDER_NAME));
      }
  
      private void testPath(File path) {
          String TAG = "Debug.MainActivity.java";
          String FOLDER_CREATION_SUCCESS = " mkdir() success: ";
  
          boolean success = path.mkdir();
          Log.d(TAG, path.getAbsolutePath() + FOLDER_CREATION_SUCCESS + success);
          path.delete();
      }
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>/storage/emulated/0/TestFolder mkdir() success: true
  /storage/emulated/0/TestFolder mkdir() success: true
  /storage/emulated/1/TestFolder mkdir() success: false
  /storage/sdcard0/Download/TestFolder mkdir() success: true
  /storage/sdcard1/Pictures/TestFolder mkdir() success: false
  </code></pre>