Copy files and folders from Assets folder to memory card in android

Copy and paste the below methods in your Activity class code.
 
static String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
 final static String TARGET_BASE_PATH = extStorageDirectory+"/My Demo Folder/";

private void copyFilesToSdCard() {
     copyFileOrDir("");
 }

 private void copyFileOrDir(String path) {
     AssetManager assetManager = this.getAssets();
     String assets[] = null;
     try {
         Log.i("tag", "copyFileOrDir() "+path);
         assets = assetManager.list(path);
         if (assets.length == 0) {
             copyFile(path);
         } else {
             String fullPath =  TARGET_BASE_PATH + path;
             Log.i("tag", "path="+fullPath);
             File dir = new File(fullPath);
             if (!dir.exists())
                 if (!dir.mkdirs());
                     Log.i("tag", "could not create dir "+fullPath);
             for (int i = 0; i < assets.length; ++i) {
                 String p;
                 if (path.equals(""))
                     p = "";
                 else 
                     p = path + "/";

                     copyFileOrDir( p + assets[i]);
             }
         }
     } catch (IOException ex) {
         Log.e("tag", "I/O Exception", ex);
     }
 }

 private void copyFile(String filename) {
     AssetManager assetManager = this.getAssets();

     InputStream in = null;
     OutputStream out = null;
     String newFileName = null;
     try {
         Log.i("tag", "copyFile() "+filename);
         in = assetManager.open(filename);
         if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
             newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4);
         else
             newFileName = TARGET_BASE_PATH + filename;
         out = new FileOutputStream(newFileName);

         byte[] buffer = new byte[1024];
         int read;
         while ((read = in.read(buffer)) != -1) {
             out.write(buffer, 0, read);
         }
         in.close();
         in = null;
         out.flush();
         out.close();
         out = null;
     } catch (Exception e) {
         Log.e("tag", "Exception in copyFile() of "+newFileName);
         Log.e("tag", "Exception in copyFile() "+e.toString());
     }

 }

Then call "copyFilesToSdCard()" method from onCreate or from any button on click event.

If you want files should be copied when application start then call "copyFilesToSdCard()" method on "onCreate".

So the full activity code will look like this:

public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
   copyFilesToSdCard();
  
 }
 
 
 static String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
 final static String TARGET_BASE_PATH = extStorageDirectory+"/My Demo Folder/";

 private void copyFilesToSdCard() {
     copyFileOrDir("");
 }

 private void copyFileOrDir(String path) {
     AssetManager assetManager = this.getAssets();
     String assets[] = null;
     try {
         Log.i("tag", "copyFileOrDir() "+path);
         assets = assetManager.list(path);
         if (assets.length == 0) {
             copyFile(path);
         } else {
             String fullPath =  TARGET_BASE_PATH + path;
             Log.i("tag", "path="+fullPath);
             File dir = new File(fullPath);
             if (!dir.exists())
                 if (!dir.mkdirs());
                     Log.i("tag", "could not create dir "+fullPath);
             for (int i = 0; i < assets.length; ++i) {
                 String p;
                 if (path.equals(""))
                     p = "";
                 else 
                     p = path + "/";

                     copyFileOrDir( p + assets[i]);
             }
         }
     } catch (IOException ex) {
         Log.e("tag", "I/O Exception", ex);
     }
 }

 private void copyFile(String filename) {
     AssetManager assetManager = this.getAssets();

     InputStream in = null;
     OutputStream out = null;
     String newFileName = null;
     try {
         Log.i("tag", "copyFile() "+filename);
         in = assetManager.open(filename);
         if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
             newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4);
         else
             newFileName = TARGET_BASE_PATH + filename;
         out = new FileOutputStream(newFileName);

         byte[] buffer = new byte[1024];
         int read;
         while ((read = in.read(buffer)) != -1) {
             out.write(buffer, 0, read);
         }
         in.close();
         in = null;
         out.flush();
         out.close();
         out = null;
     } catch (Exception e) {
         Log.e("tag", "Exception in copyFile() of "+newFileName);
         Log.e("tag", "Exception in copyFile() "+e.toString());
     }

 }
 }

You must include this permisson in your AndroidManifest.xml :



Comments

Popular posts from this blog

Android Sudoku Game Project With Source Code