Android copy files from assets to datafile in Internal Storage.

Copying files from assets folder to internal storage in Android is not a difficult task. It consists of three parts:

Copying-files-from-assets-folder-to-internal-storage
Assets folder directory structure

How to copy files from assets to Internal Storage Android – 

  1. First make sure the assets folder is in the correct location. The correct location of Assets folder in Android is youappnameappsrcmain.
  2. Creating a file in the internal storage where you will write the file that is copied from the assets folder.
  3. Finally copying the file.

Here is the sample code for copying a file called yourdata.extension from the assets folder to the internal storage in a file called yourapp – 
 
        Context context = getApplicationContext();
        String path = Environment.getExternalStorageDirectory()+"/yourapp/";
        File file = new File(path+ "yourdata.extension");
        if (!file.exists()) {
            dir.mkdirs();
            copyFile(context);
        }


                private void copyFile(Context context) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStream in = assetManager.open("eng.traineddata");
            OutputStream out = new FileOutputStream(path+"yourdata.extension");
            byte[] buffer = new byte[1024];
            int read = in.read(buffer);
            while (read != -1) {
                out.write(buffer, 0, read);
                read = in.read(buffer);
               }
            } catch (Exception e) {
            e.getMessage();
          }
        } 

So we’ve seen that copying files from assets to internal storage is pretty easy in Android, you just have to make sure that the assets folder is in the correct directory. This might be confusing at first as earlier, the location of assets folder was different, but now the location of assests folder is in the main folder of your app, i.e – yourappnameappsrcmain. This sums up how to copy files from assets folder to internal storage in Android. If you have any question, feel free to drop them below. Happy coding!

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Sharing is caring!

Leave a Comment

Your email address will not be published.