package com.basic.security.utils; import android.content.res.AssetManager; import android.util.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class AssetHelper { static String TARGET_BASE_PATH = "/sdcard/appname/voices/"; // private static 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); // } // } public static void copyAssets(AssetManager assetManager, String outDir) { TARGET_BASE_PATH = outDir + "/"; copyFileOrDir(assetManager, ""); // copy all files in assets folder in my project // String[] files = null; // try { // files = assetManager.list(""); // } catch (IOException e) { // Log.e("tag", "Failed to get asset file list.", e); // } // for(String filename : files) { // InputStream in = null; // OutputStream out = null; // try { // System.out.println("filename="+filename+",outDir="+outDir); // in = assetManager.open(filename); // //String outDir = c.getFilesDir().getAbsolutePath() + "/X/Y/Z/" ; // // File outFile = new File(outDir, filename); // // out = new FileOutputStream(outFile); // copyFile(in, out); // in.close(); // in = null; // out.flush(); // out.close(); // out = null; // } catch(Exception e) { // //Log.e("tag", "Failed to copy asset file: " + filename, e); // } // } } private static void copyFileOrDir(AssetManager assetManager, String path) { String assets[] = null; try { // Log.i("tag", "copyFileOrDir() "+path); assets = assetManager.list(path); if (assets.length == 0) { copyFile(assetManager, path); } else { String fullPath = TARGET_BASE_PATH + path; // Log.i("tag", "path="+fullPath); File dir = new File(fullPath); if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) 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 + "/"; if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) copyFileOrDir(assetManager, p + assets[i]); } } } catch (IOException ex) { Log.e("tag", "I/O Exception", ex); } } private static void copyFile(AssetManager assetManager, String filename) { 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()); } } }