title: Copy file from the internal to the external storage in Android
body: <p>My app (<em>Android API 15</em>) makes a picture and stores it in the internal memory's folder. Now, I want to copy this file to another folder inside of the external storage, e.g. <code>/sdcard/myapp</code>. I tried the following approaches:</p>
  
  <p><strong>Approach #1:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>private void copyFile(File src, File dst) throws IOException {
  
      File from = new File(src.getPath());
      File to = new File(dst.getPath());
      from.renameTo(to);
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Approach #2:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>private void copyFile(File src, File dst) throws IOException {
  
      FileChannel inChannel = null;
      FileChannel outChannel = null;
  
      try {
          inChannel = new FileInputStream(src).getChannel();
          outChannel = new FileOutputStream(dst).getChannel();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  
      try {
          inChannel.transferTo(0, inChannel.size(), outChannel);
      } finally {
          if (inChannel != null)
              inChannel.close();
          if (outChannel != null)
              outChannel.close();
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Approach #3:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>private void copyFile(File src, File dst) throws IOException {
  
      FileInputStream inStream = new FileInputStream(src);
  
      if (!dst.exists()) {
          dst.mkdir();
      }
  
      if (!dst.canWrite()) {
          System.out.print("CAN'T WRITE");
          return;
      }
  
      FileOutputStream outStream = new FileOutputStream(dst);
      FileChannel inChannel = inStream.getChannel();
      FileChannel outChannel = outStream.getChannel();
      inChannel.transferTo(0, inChannel.size(), outChannel);
      inStream.close();
      outStream.close();
  }
  </code></pre>
  
  <hr>
  
  <p>None of these methods doesn't solve my task. In checked a number of related topics, and the only suggestion I found is to verify the persistence of </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>in <code>AndroidManifest.xml</code> and it does persist.</p>
  
  <p>The <strong><em>approach #1</em></strong> finishes the execution, but no folder and files are copied.</p>
  
  <p>In the <strong><em>approach #2</em></strong>, the app fails with the exception <code>java.lang.NullPointerException</code> at <code>outChannel = new FileOutputStream(dst).getChannel();</code>, but the object dst is not a null.</p>
  
  <p>In the <strong><em>approach #3</em></strong>, I decided to verify if the destination object exists and it creates a folder if needed, but when I check if I can write, the check returns <code>false</code>.</p>
  
  <p>I tried a couple of additional approaches, which succeeded to create an empty folder, but no files are really copied.</p>
  
  <p>Since this is my very first step towards Android, I feel I miss some small thing. Please, point me, how to copy a file from one folder to another folder in Android, including file moving from internal to external memory.</p>