title: Android M write to SD Card - Permission Denied
body: <p>I'm trying to copy file from within my application to the SD card, but I get the error <strong>eacces (permission denied)</strong>. The OS is Android M and I have allowed runtime Storage permissions (checked in app info). I have also set the uses-permission in AndroidManifest.xml</p>
  
  <pre><code>&lt;application&gt;...&lt;/application&gt;
  &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>Doesn't work if I copy to SD card</p>
  
  <pre><code>Source: data/user/0/com.example.myapp/cache/SomeFile.txt
  Destination: /storage/1032-2568/SomeFolder/
  Error: java.io.FileNotFoundException: /storage/1032-2568/SomeFolder/SomeFile.txt: open failed: EACCES (Permission denied)
  </code></pre>
  
  <p>Works if I copy to internal storage</p>
  
  <pre><code>Source: data/user/0/com.example.myapp/cache/SomeFile.txt
  Destination: /storage/emulated/0/SomeFolder/
  </code></pre>
  
  <p>Code to copy file from source to destination</p>
  
  <pre><code>/*
   * Below are the parameters I have tried
   *
   * inputPath - data/user/0/com.example.myapp/cache or data/user/0/com.example.myapp/cache/
   * inputFile - /SomeFile.txt or SomeFile.txt
   * outputPath - /storage/1032-2568/SomeFolder/ or /storage/1032-2568/SomeFolder
   */
  public static void copyFile(String inputPath, String inputFile, String outputPath) {
  
      InputStream in = null;
      OutputStream out = null;
      try {
  
          //create output directory if it doesn't exist
          File dir = new File (outputPath);
          if (!dir.exists()) {
              dir.mkdirs();
          }
  
          in = new FileInputStream(inputPath + inputFile);
          out = new FileOutputStream(outputPath + inputFile);
  
          byte[] buffer = new byte[1024];
          int read;
          while ((read = in.read(buffer)) != -1) {
              out.write(buffer, 0, read);
          }
          in.close();
  
  
          // write the output file (You have now copied the file)
          out.flush();
          out.close();
      }
      catch (FileNotFoundException fnfe1) {
          /* I get the error here */
          Log.e("tag", fnfe1.getMessage());
      }
      catch (Exception e) {
          Log.e("tag", e.getMessage());
      }
  }
  </code></pre>
  
  <h2>ES File Explorer</h2>
  
  <p>I saw that ES File Explorer also cannot write anything on the SD Card on Redmi devices. <a href="https://youtu.be/q0w3kZm3XA4" rel="noreferrer">Here's a video with solution</a>. Following the steps worked for ES Explorer on my device. Can this be done programmatically?</p>