title: Can't create folder on external storage on android
body: <p>My development phone is a <code>Nexus 5</code>, running <code>Android 4.4.2</code>.</p>
  
  <p>In my application, I am attempting to create a folder on external storage that will store debug information for my application. Basically it will contain all the commands executed by the application, so that when a user encounters a problem, I have the option of having them send me the information from the debug folder to analyse. </p>
  
  <p>I started off by trying to write a file to the folder, but found there was an error creating the folder. At first I was using <code>mkdir()</code>, then I moved onto <code>mkdirs()</code> which also didn't work. </p>
  
  <p>I have <code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;</code> in my manifest. </p>
  
  <p>Here is the code for creating the folder:</p>
  
  <pre><code>    File folder = new File(Environment.getExternalStorageDirectory() + "/DebugData");
  
      String path = folder.getPath();
  
      if(!folder.mkdirs() || !folder.exists()){        
              Log.e(LOG_TAG, path + " failed");
          } else {
              Log.d(LOG_TAG, path + " succeeded");
          } 
  </code></pre>
  
  <p>Here is what I have also tried:</p>
  
  <pre><code>    //Check SD card state
      String state = Environment.getExternalStorageState();
      if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || !Environment.MEDIA_MOUNTED.equals(state)) {
          Log.e(LOG_TAG, "Error: external storage is read only or unavailable");
      } else {
          Log.d(LOG_TAG, "External storage is not read only or unavailable");
      }
  </code></pre>
  
  <p>This returns that the external storage is <strong>not</strong> read only or unavailable.</p>
  
  <p>I have also tried different paths, such as <code>File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Folder1");</code></p>
  
  <p><strong>This is where it became really confusing.</strong></p>
  
  <p>I tried development on different phones. Firstly, I grabbed a <code>Galaxy S4 GT-i9505</code> running <code>Android 4.2.2</code> and it worked. I was able to create the folders and write to them. This showed me that the code was working. Also the path returned by running the code on the S4 and Nexus 5 was the same. </p>
  
  <p>Then I thought it may be android version specific. So I grabbed a <code>Nexus 4</code> with <code>Android 4.4.2</code> and the code worked on it as well. Created the folders and allowed me to write to them. </p>
  
  <p>None of the phones are rooted and are all stock standard. There's no special applications or anything I can think of settings wise on the Nexus 5 that would cause permissions problems. The connection is set to Media Device (MTP). </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I should add that I have tried the follow which also did not work:</p>
  
  <ul>
  <li>Writing a file to the root directory of the external storage</li>
  <li>Creating the file in the external storage root directory and writing to it</li>
  <li>Creating a folder in a path outlined and writing a file to it</li>
  <li>Creating the file in the path outlined and writing to it</li>
  </ul>
  
  <p>I am confused as to what is causing this, is there anything else I can test or change to fix the issue? </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Turns out the issue was due to, I think, indexing.</p>
  
  <p>Basically all of the other devices I tested on, allowed me to reconnect the USB connection and view the created files and folders.</p>
  
  <p>For some reason my Nexus 5 doesn't index the folders/files, even though they exist.</p>
  
  <p>I downloaded a different 3rd party file explorer application and noticed all the folders and files were there.</p>
  
  <p>So to view these folders and files via USB debugging, I have to restart the phone in order to re-index them, which seems quite annoying but it is better than it not working at all.</p>
  
  <p>Thanks. </p>