Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

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

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

From source file:ca.uqac.florentinth.speakerauthentication.Utils.JSONUtils.java

public static String parseJSONFile(Context ctx, String file) {
    String json = null;/*from  w w  w. j a v a 2  s .co  m*/

    try {
        InputStream inputStream = ctx.getAssets().open(file, AssetManager.ACCESS_STREAMING);
        int size = inputStream.available();
        byte[] buffer = new byte[size];
        inputStream.read(buffer);
        inputStream.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }

    return json;
}

From source file:Main.java

public static void copyAssetDirToFiles(Context context, String root, String dirname) throws IOException {
    File dir = new File(root + "/" + dirname);
    dir.mkdir();/*from w w  w. ja  v  a 2s  . c  o m*/

    AssetManager assetManager = context.getAssets();
    String[] children = assetManager.list(dirname);
    for (String child : children) {
        child = dirname + '/' + child;
        String[] grandChildren = assetManager.list(child);
        if (0 == grandChildren.length)
            copyAssetFileToFiles(context, root, child);
        else
            copyAssetDirToFiles(context, root, child);
    }
}

From source file:Main.java

/**
 * Sets a determined font on a text view element
 *
 * @param context   Context in which the TextView can be found
 * @param font      Font to be set in the text view see available fonts as static attributes of this class
 * @param style     {@see Typeface}//from w ww . j  a v  a  2  s . co  m
 * @param textViews TextViews to which the font will be applied
 */
public static void setTypeface(Context context, String font, int style, TextView... textViews) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    for (TextView txt : textViews) {
        txt.setTypeface(tf, style);
    }
}

From source file:Main.java

/**
 * Sets a determined font on a text view element
 *
 * @param context Context in which the TextView can be found
 * @param font    Font to be set in the text view see available fonts as static attributes of this class
 * @param style   {@see Typeface}/* www  . ja v  a2  s  . c  o  m*/
 * @param group   Root layout in which TextView and Buttons will be searched to apply the font
 */
public static void setTypeface(Context context, String font, int style, ViewGroup group) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    int count = group.getChildCount();
    View v;
    for (int i = 0; i < count; i++) {
        v = group.getChildAt(i);
        if (v instanceof TextView)
            ((TextView) v).setTypeface(tf, style);
        else if (v instanceof ViewGroup)
            setTypeface(context, font, style, (ViewGroup) v);
    }
}

From source file:Main.java

public static File getRobotCacheFile(Context context, String assetname) throws IOException {
    File cacheFile = new File(context.getCacheDir(), assetname);
    try {/*from   ww  w.j  a v  a  2 s  .  c om*/
        InputStream inputStream = context.getAssets().open(assetname);
        try {
            FileOutputStream outputStream = new FileOutputStream(cacheFile);
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new IOException("Could not open " + assetname, e);
    }
    return cacheFile;
}

From source file:Main.java

static public boolean copyFileTo(Context c, String orifile, String desfile) throws IOException {
    InputStream myInput;/*w ww . j a v a  2 s  . c o  m*/
    OutputStream myOutput = new FileOutputStream(desfile);
    myInput = c.getAssets().open(orifile);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while (length > 0) {
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }

    myOutput.flush();
    myInput.close();
    myOutput.close();

    return true;
}

From source file:Main.java

private static Bitmap decodeBitmap(Context context, String frameName, int position) {
    try {// ww  w  .  j  a va  2  s  .  co  m
        switch (position) {
        case 0:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/leftup.png"));
        case 1:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/left.png"));
        case 2:
            return BitmapFactory
                    .decodeStream(context.getAssets().open("frames/" + frameName + "/leftdown.png"));
        case 3:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/down.png"));
        case 4:
            return BitmapFactory
                    .decodeStream(context.getAssets().open("frames/" + frameName + "/rightdown.png"));
        case 5:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/right.png"));
        case 6:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/rightup.png"));
        case 7:
            return BitmapFactory.decodeStream(context.getAssets().open("frames/" + frameName + "/up.png"));
        default:
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.guipenedo.pokeradar.Utils.java

public static String loadJSONFromFile(Context context, String filename) {
    String json;/*from w w  w.  j a v a  2  s .co m*/
    try {
        InputStream is = context.getAssets().open(filename);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

public static void copyDatabase2FileDir(Context context, String dbName) {
    InputStream stream = null;/*w  ww.j  av  a2  s.c om*/
    BufferedOutputStream outputStream = null;
    try {

        stream = context.getAssets().open(dbName);
        File file = new File(context.getFilesDir(), dbName);

        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
            outputStream.flush();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/** load the Bitmap from assets or sdcard_dir
 * @param context//w  w w .  j a  v a2 s  .c o  m
 * @param outsideFileName
 * @param op
 * @param useSdcard
 * @param sdcard_assetdir_path
 * @return
 */
public static Bitmap loadBitmap(Context context, String outsideFileName, BitmapFactory.Options op,
        boolean useSdcard, String sdcard_assetdir_path) {
    AssetManager am = context.getAssets();

    Bitmap retval = null;
    synchronized (sCacheMap) {
        if (sCacheMap.get(outsideFileName) != null) {
            retval = sCacheMap.get(outsideFileName);
            return retval;
        }
    }

    try {
        retval = BitmapFactory.decodeStream(am.open(outsideFileName), null, op);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (useSdcard) {
        Bitmap _tmp = BitmapFactory.decodeFile(sdcard_assetdir_path + outsideFileName, op);
        if (_tmp != null) {
            retval = _tmp;
        }
    }

    if (retval != null) {
        synchronized (sCacheMap) {
            sCacheMap.put(outsideFileName, retval);
        }

    }

    return retval;
}