title: Android Mkdir Eacces Permission Denied
body: <p>I need to help for <code>mkdir failed: EACCES (Permission denied)</code></p>
  
  <p>My build setting;
  minSdkVersion 15 - targetSdkVersion 19 - compileSdkVersion 25 -
   buildToolsVersion "25.0.0"</p>
  
  <p>I use Android Studio 2.3</p>
  
  <p>Debug Device : Samsung J1 (SM-J200F) Android 5.1.1 Android Security Patch Level 2016-12-01</p>
  
  <p>and </p>
  
  <p>Sony Xperia Z5 (E6603) Android 7.0 Android Security Patch Level 2016-12-01</p>
  
  <p>But Google Play APK version is working</p>
  
  <p>Android Manifest;</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  </code></pre>
  
  <p>Debug Log;</p>
  
  <pre><code>    Line 124 : `FileOutputStream out = new FileOutputStream(file);`
  
      `03-23 10:59:06.096 24568-24788/com.myAppName I/System.out: AsyncTask #1 calls detatch()
      03-23 10:59:06.106 24568-24568/com.myAppName W/System.err: mkdir failed: EACCES (Permission denied) : /storage/emulated/0/MyAppName
      03-23 10:59:06.106 24568-24568/com.myAppName D/Failed: Open
      03-23 10:59:06.106 24568-24568/com.myAppName W/System.err: java.io.FileNotFoundException: /storage/emulated/0/MyAppName/MyAppName-6662.jpg: open failed: ENOENT (No such file or directory)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:87)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:72)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.SaveImage(FileDownloadHelper.java:124)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.onPostExecute(FileDownloadHelper.java:59)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.onPostExecute(FileDownloadHelper.java:27)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:636)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask.access$500(AsyncTask.java:177)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.Looper.loop(Looper.java:145)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6946)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.Posix.open(Native Method)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
      03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:  ... 15 more`
  </code></pre>
  
  <p>My save function;</p>
  
  <pre><code>    private Bitmap SaveImage(Bitmap result) {
          String filename = "";
          File instaFolder = new File(Environment.getExternalStorageDirectory().toString()
                  + "/" + Constants.FOLDER_NAME);
          if (!instaFolder.exists())
              instaFolder.mkdir();
  
          Random generator = new Random();
          int n = 10000;
          n = generator.nextInt(n);
          filename = Constants.FOLDER_NAME + "-" + n + ".jpg";
          file = new File(instaFolder, filename);
          if (file.exists()) file.delete();
          _mImageUrl = file.getPath();
          try {
              FileOutputStream out = new FileOutputStream(file);
              result.compress(Bitmap.CompressFormat.JPEG, 100, out);
              out.flush();
              out.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return result;
      }
  </code></pre>