title: How to copy assets sub-folders files on sd-card in android
body: <p>I want to copy the files and folders of my assets folder on a specified folder on sd-card. Here's my code:</p>
  
  <pre><code>private void copyFiles() {
  
      AssetManager assetManager = getAssets();
      InputStream in = null;
      OutputStream out = null;
  
      try
      {
          String files[] = assetManager.list("Files");
  
          for (String fileName : files)
          {
              Log.d("File name =&gt;", fileName);
  
              in = assetManager.open("Files/" + fileName);
  
              out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Files/" + fileName);
  
              copyFile(in, out);
  
              out.flush();
          }
  
          in.close();
          out.close();
      }
      catch (IOException e) { e.printStackTrace(); }
  }
  
  private void copyFile(InputStream in, OutputStream out) throws IOException
  {
      byte[] buffer = new byte[1024];
      int read;
  
      while ((read = in.read(buffer)) != -1) out.write(buffer, 0, read);
  }
  </code></pre>
  
  <p>The problem is this method does not copy sub-folders items of the assets on sd-card. In fact on the first occurrence it catches IO exception. here's my assets folder structure:</p>
  
  <p><img src="https://i.stack.imgur.com/N5lrg.png" alt="enter image description here"></p>
  
  <p>Here is the LogCat error message:</p>
  
  <pre><code>01-12 20:13:51.692: W/System.err(12798): java.io.FileNotFoundException: Files/01
  01-12 20:13:51.742: W/System.err(12798):    at android.content.res.AssetManager.openAsset(Native Method)
  01-12 20:13:51.742: W/System.err(12798):    at android.content.res.AssetManager.open(AssetManager.java:325)
  01-12 20:13:51.742: W/System.err(12798):    at android.content.res.AssetManager.open(AssetManager.java:299)
  01-12 20:13:51.742: W/System.err(12798):    at com.handydic.view.WelcomeScreen.copyDefaultTheme(WelcomeScreen.java:105)
  01-12 20:13:51.742: W/System.err(12798):    at com.handydic.view.WelcomeScreen.setup(WelcomeScreen.java:70)
  01-12 20:13:51.742: W/System.err(12798):    at com.handydic.view.WelcomeScreen.onCreate(WelcomeScreen.java:38)
  01-12 20:13:51.742: W/System.err(12798):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
  01-12 20:13:51.742: W/System.err(12798):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
  01-12 20:13:51.742: I/RosieUtility(12838): enable self killer: mEnabled=0
  01-12 20:13:51.742: W/System.err(12798):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
  01-12 20:13:51.742: W/System.err(12798):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
  01-12 20:13:51.752: W/System.err(12798):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
  01-12 20:13:51.752: W/System.err(12798):    at android.os.Handler.dispatchMessage(Handler.java:99)
  01-12 20:13:51.752: W/System.err(12798):    at android.os.Looper.loop(Looper.java:150)
  01-12 20:13:51.752: W/System.err(12798):    at android.app.ActivityThread.main(ActivityThread.java:4385)
  01-12 20:13:51.752: W/System.err(12798):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-12 20:13:51.752: W/System.err(12798):    at java.lang.reflect.Method.invoke(Method.java:507)
  01-12 20:13:51.752: W/System.err(12798):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
  01-12 20:13:51.752: W/System.err(12798):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
  01-12 20:13:51.762: W/System.err(12798):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The forth line in LogCat output "copyDefaultTheme()" indicates line 105 which is where I have written "in = assetManager.open("Files/" + fileName);"</p>
  
  <p>Any idea what to do? Thanks.</p>