title: File copy from raw to External (secondary) SD card
body: <p>I know that many question has been answered, 
  <strong>but in my case my code is working properly on Oppo, samsung phone but not work on MI, MOto G, Lenavo phone</strong> </p>
  
  <hr>
  
  <p><strong>Here is my code:</strong></p>
  
  <p>This String return path of secondary SD card:</p>
  
  <pre><code>public static String getExternalStorage() {
          File rootFolder = new File("/");
          boolean isSdcardRemovable = false;
          String path = null;
       /* loop: */
       for (int i = 0; i &lt; rootFolder.listFiles().length; i++) {
  
          if (rootFolder.listFiles()[i].listFiles() != null
                  &amp;&amp; !rootFolder.listFiles()[i].toString().contains("system")
                  &amp;&amp; !rootFolder.listFiles()[i].toString().contains("etc")
                  &amp;&amp; !rootFolder.listFiles()[i].toString().contains("dev")) {
  
              File dataDir = new File(Environment.getDataDirectory()
                      .getAbsolutePath());
  
              long dataDirSize = dataDir.getFreeSpace() / (1000 * 1000);
              long folderSize = rootFolder.listFiles()[i].getFreeSpace()
                      / (1000 * 1000);
  
              if (dataDirSize == folderSize
                      || (dataDirSize &gt; folderSize &amp;&amp; folderSize &gt; (dataDirSize - 80))) {
                  System.err
                          .println("INTERNAL1 " + rootFolder.listFiles()[i]);
                  System.err.println(dataDirSize);
                  System.err.println(folderSize);
              } else {
  
                  File rootSubFolder1 = new File(
                          rootFolder.listFiles()[i].getAbsolutePath());
  
                  if (rootSubFolder1.listFiles() != null) {
  
                      for (int j = 0; j &lt; rootSubFolder1.listFiles().length; j++) {
  
                          if (rootSubFolder1.listFiles()[j].getTotalSpace() != 0
                                  &amp;&amp; rootSubFolder1.listFiles()[j]
                                          .getFreeSpace() != 0
                                  &amp;&amp; rootSubFolder1.listFiles()[j]
                                          .listFiles() != null) {
  
                              Debug.i("fromGetExternalStorage", ""
                                      + rootSubFolder1.listFiles()[j]);
  
                              if (rootSubFolder1.listFiles()[j].toString()
                                      .contains("sdcard")
                                      || rootSubFolder1.listFiles()[j]
                                              .toString().contains("storage")
                                      || rootSubFolder1.listFiles()[j]
                                              .toString().contains("mnt")) {
  
                                  folderSize = rootSubFolder1.listFiles()[j]
                                          .getFreeSpace() / (1000 * 1000);
  
                                  if (dataDirSize == folderSize
                                          || (dataDirSize &gt; folderSize &amp;&amp; folderSize &gt; (dataDirSize - 80))) {
                                      System.err
                                              .println("INTERNAL2 "
                                                      + rootSubFolder1
                                                              .listFiles()[j]);
                                      System.err.println(dataDirSize);
                                      System.err.println(folderSize);
                                  } else {
                                      int pos = rootSubFolder1.listFiles()[j]
                                              .getAbsolutePath().lastIndexOf(
                                                      '/');
                                      String str = rootSubFolder1.listFiles()[j]
                                              .getAbsolutePath().substring(
                                                      pos + 1);
  
                                      if (str.matches("(sd|ext|3039|m_external_sd).*")) {
  
                                          isSdcardRemovable = true;
                                          System.err.println("EXTERNAL "
                                                  + rootSubFolder1
                                                          .listFiles()[j]);
                                          System.err.println(dataDirSize);
                                          System.err.println(folderSize);
                                          path = rootSubFolder1.listFiles()[j]
                                                  .getAbsolutePath() + "/";
                                          break loop;
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  
      if (isSdcardRemovable) {
          if (path != null) {
              Debug.i("new Path from getExternal Storage", path);
          } else {
              Debug.i("fail", "External memory not found.");
          }
      } else {
          Debug.i("fail", "External memory not available.");
  
      }
  
      return path;
  }` 
  </code></pre>
  
  <p>And This Code that I use path and copy file:</p>
  
  <blockquote>
    <p>OutputStream OS = new FileOutputStream(path + File.separator + "name.txt");</p>
  </blockquote>