title: /storage/emulated/0/ android.system.ErrnoException: open failed: EACCES (Permission denied)
body: <p>I found a knotty problem of function which is auto download new version application </p>
  
  <p>1.main code of Activity Section  :</p>
  
  <pre><code>    private void downloadFile(String url)  {
  
  
         String dir =  FileUtils.getSDCardPath()+ "/";
  
          String fileName = System.currentTimeMillis() + ".apk";
  
          path=dir+fileName;
  
          verifyStoragePermissions(UpdateActivity.this);
          if (shouldAskPermissions()) {
              askPermissions();
          }
  
          OkHttpUtils
                  .get()
                  .url(url)
                  .build().execute(
                  new FileCallBack(dir,fileName) {
  
                      @Override
                      public void onBefore(Request request, int id) {
                          super.onBefore(request, id);
  
  
  
                      }
  
                      @Override
                      public void inProgress(float progress, long total, int id) {
                         // super.inProgress(progress, total, id);
                          int progressl=(int) (100 * progress);
                          L.i("toal "+total);
                          L.i("prgress :"+progressl);
  
                          Message msg=new Message();
                          msg.what=HANDLER_LODING;
  
                          Bundle bundle=new Bundle();
                          bundle.putLong("total",total);
                          bundle.putInt("progress",progressl);
                          msg.setData(bundle);
                          handler.sendMessage(msg);
  
                      }
  
                      @Override
                      public void onError(Call call, Exception e, int id) {
  
                          L.i("error" +e.getMessage().toString()+" local:"+e.getLocalizedMessage()+" cause "+e.getCause()+" code"+e.getStackTrace());
                          UtilTools.sendEmail(getClass().toString()+"失败 e:"+e.getMessage());
                          handler.sendEmptyMessage(HANDLER_ON);
                      }
  
                      @Override
                      public void onResponse(File response, int id) {
  //                        OpenFileTipDialog.openFiles(downloadFile.getAbsolutePath(),YearShuilddjhActivity.this);
                          Log.e("info: ", "onResponse :" + response.getAbsolutePath());
                          L.i("success");
                          handler.sendEmptyMessage(HANDLER_OK);
                      }
                  }
  
          );
  
  
      }
  </code></pre>
  
  <p>2 in AndroidMenifest.xml</p>
  
  <pre><code>  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /&gt;
      &lt;uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /&gt;
  // all user permission about download file  
  </code></pre>
  
  <ol start="3">
  <li>log cat show me </li>
  </ol>
  
  <pre><code>    03-15 15:32:25.648 11709-11709/com.example.darren.fdc I/darrenli: error/storage/emulated/0/1552635145458.apk: open failed: EACCES (Permission denied) local:/storage/emulated/0/1552635145458.apk: open failed: EACCES (Permission denied) cause android.system.ErrnoException: open failed: EACCES (Permission denied) code[Ljava.lang.StackTraceElement;@2d58e5b8
  </code></pre>
  
  <ol start="4">
  <li>build.gradle</li>
  </ol>
  
  <pre><code>  api 'com.squareup.picasso:picasso:2.5.2'
    api 'com.zhy:okhttputils:2.6.2'
  
      //RxVolley
      api('com.kymjs.rxvolley:rxvolley:1.1.2') {
          exclude group: 'com.squareup.okhttp3', module: 'okhttp'
          exclude group: 'com.squareup.okio'
          exclude group: 'io.reactivex'
      }
      //RxVolley下载模块
      api('com.kymjs.rxvolley:okhttp:1.1.0') {
          exclude group: 'com.squareup.okhttp3'
          exclude group: 'com.squareup.okio'
      }
      //NumberProgressBar
      api 'com.daimajia.numberprogressbar:library:1.2@aar'
      api 'io.reactivex:rxandroid:1.0.1'
      api 'io.reactivex:rxjava:1.0.14'
  </code></pre>
  
  <p>After that I performed some tests:</p>
  
  <p>1st try:  </p>
  
  <pre><code> private static final int REQUEST_EXTERNAL_STORAGE = 1;
      private static String[] PERMISSIONS_STORAGE = {
              Manifest.permission.READ_EXTERNAL_STORAGE,
              Manifest.permission.WRITE_EXTERNAL_STORAGE };
  
      public  void verifyStoragePermissions(Activity activity) {
                  // Check if we have write permission
                 int permission = ActivityCompat.checkSelfPermission(activity,
                                  Manifest.permission.WRITE_EXTERNAL_STORAGE);
                 L.i("error" +permission);
          L.i("error1" + PackageManager.PERMISSION_GRANTED);
                 if (permission != PackageManager.PERMISSION_GRANTED) {
                          // We don't have permission so prompt the user
                         ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                                          REQUEST_EXTERNAL_STORAGE); } }
  </code></pre>
  
  <p>//call for my download code </p>
  
  <p>2nd try:</p>
  
  <pre><code> protected boolean shouldAskPermissions() {
          return (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.LOLLIPOP_MR1);
      }
  
      @TargetApi(23)
      protected void askPermissions() {
          String[] permissions = {
                  "android.permission.READ_EXTERNAL_STORAGE",
                  "android.permission.WRITE_EXTERNAL_STORAGE"
          };
          int requestCode = 200;
          requestPermissions(permissions, requestCode);
      }
  </code></pre>
  
  <p>3rd try</p>
  
  <pre><code>public static boolean RootCommand(String command){
  
          Process process = null;
          DataOutputStream os = null;
  
          try{
              process = Runtime.getRuntime().exec("su");
              os = new DataOutputStream(process.getOutputStream());
              os.writeBytes(command + "\n");
              os.writeBytes("exit\n");
              os.flush();
              process.waitFor();
          } catch (Exception e){
              return false;
          } finally{
              if (os != null){
                  try {
                      os.close();
                  }catch (IOException e){
                      e.printStackTrace();
                  }
              }
              if(process!=null) {
                  process.destroy();
              }
          }
          return true;
      }
  
  
     String apkRoot="chmod 777 "+path;
  </code></pre>
  
  <p>UpdateActivity.RootCommand(apkRoot);</p>