I have a folder that contains several files and some directories that I need to copy to the SD card / mnt / sdcard / Android / data / path when I launch the application for the first time, and, of course, if there was no required folder yet missing in this way.
I will have this folder inside the res / raw folder of my application.
What are the step-by-step procedures that I need to do so that I can copy the folder and all its contents from res / raw to the specified path on the SD card.
Any help is greatly appreciated.
Edit
Below is a solution if it helps someone else:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); copyFileOrDir("edu1");//directory name in assets } File sdCard = Environment.getExternalStorageDirectory(); private void copyFileOrDir(String path) { AssetManager assetManager = this.getAssets(); String assets[] = null; try { assets = assetManager.list(path); if (assets.length == 0) { copyFile(path); } else { File dir = new File (sdCard.getAbsolutePath() + "/" + "Android/data"); //String fullPath = "/data/data/" + this.getPackageName() + "/" + path;//path for storing internally to data/data //File dir = new File(fullPath); if (!dir.exists()){ System.out.println("Created directory"+sdCard.getAbsolutePath() + "/Android/data"); boolean result = dir.mkdir(); System.out.println("Result of directory creation"+result); } for (int i = 0; i < assets.length; ++i) { copyFileOrDir(path + "/" + assets[i]); } } } catch (IOException ex) { System.out.println("Exception in copyFileOrDir"+ex); } } private void copyFile(String filename) { AssetManager assetManager = this.getAssets(); InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); //String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;//path for storing internally to data/data String newFileName = sdCard.getAbsolutePath() + "/Android/data/" + 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) { System.out.println("Exception in copyFile"+e); } } }
source share