Example usage for android.content ContextWrapper getAssets

List of usage examples for android.content ContextWrapper getAssets

Introduction

In this page you can find the example usage for android.content ContextWrapper getAssets.

Prototype

@Override
    public AssetManager getAssets() 

Source Link

Usage

From source file:com.quinsoft.zeidon.android.AndroidUtils.java

/**
 * Checks to see if the targetDbName exists.  If not it will be copied from emptyDbName,
 * which must be in the assets directory.
 *
 * @param context//w ww.ja va 2s  . c  o m
 * @param emptyDbName
 * @param targetDbName
 */
public static void initializeDatabase(ContextWrapper context, String emptyDbName, String targetDbName) {
    context.getExternalFilesDir(null); // Create files directory if necessary.
    File f = new File(targetDbName);
    if (f.exists()) // Does DB already exist?
        return;

    Log.i("AndroidUtils.initializeDatabase",
            "Initializing DB by copying " + emptyDbName + " to " + f.getAbsolutePath());
    InputStream ins = null;
    FileOutputStream fos = null;
    try {
        ins = context.getAssets().open(emptyDbName);
    } catch (IOException e) {
        throw ZeidonException.wrapException(e).prependFilename(emptyDbName);
    }

    try {
        fos = new FileOutputStream(f);
    } catch (Exception e) {
        IOUtils.closeQuietly(ins);
        throw ZeidonException.wrapException(e).prependFilename(targetDbName);
    }

    try {
        byte[] buffer = new byte[1024];
        int size = 0;

        while ((size = ins.read(buffer, 0, buffer.length)) >= 0)
            fos.write(buffer, 0, size);
    } catch (IOException e) {
        throw ZeidonException.wrapException(e).prependFilename(emptyDbName);
    } finally {
        IOUtils.closeQuietly(ins);
        IOUtils.closeQuietly(fos);
    }
}