Android examples for App:Assets File
Copy the asset at the specified current Folder Or File to this app's data directory.
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; public class Main { public static void copyAsset(Context context, String currentFolderOrFile, String outDir) {/*from w w w . j a v a2 s . c om*/ AssetManager manager = context.getAssets(); try { String[] contents = manager.list(currentFolderOrFile); if (contents == null || contents.length == 0) throw new IOException(); // Make the directory. File dir = new File(outDir, currentFolderOrFile); dir.mkdirs(); // Recurse on the contents. for (String entry : contents) { copyAsset(context, currentFolderOrFile + "/" + entry, outDir); } } catch (IOException e) { copyFileAsset(context, outDir, currentFolderOrFile); } } /** * Copy the asset file specified by path to app's data directory. Assumes * parent directories have already been created. * * @param path * Path to asset, relative to app's assets directory. */ private static void copyFileAsset(Context context, String out_dir, String path) { File file = new File(out_dir, path); try { InputStream in = context.getAssets().open(path); OutputStream out = new FileOutputStream(file); byte[] buffer = new byte[1024 * 256]; int read = in.read(buffer); while (read != -1) { out.write(buffer, 0, read); read = in.read(buffer); } out.close(); in.close(); } catch (IOException e) { Log.e("ERROR", e.toString()); } } }