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:Main.java

/**
 * @param filePath example("pictures/picture_1.jpg")
 *//*from   w  ww.j  a  v  a2 s  .c o m*/
public static Bitmap getAssetsPicture(Context context, String filePath) {
    Bitmap bitmap = null;
    AssetManager assetManager = context.getAssets();
    try {
        InputStream inputStream = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String fileName) {
    String json;/*from w w  w.  j a  v a 2  s. c  o  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

/**
 * /*from w ww. ja v  a2  s  .com*/
 * @param context
 * @return
 */
public static Typeface getFAFont(Context context) {
    Typeface tf = null;
    try {
        tf = Typeface.createFromAsset(context.getAssets(), font_awesome);
    } catch (Exception e) {
        Log.e(TAG, "Failed to load FontAwesome Font");
        Toast.makeText(context, "Failed to load FontAwesome Font", Toast.LENGTH_SHORT).show();
    }
    return tf;
}

From source file:Main.java

public static String readAssetsFileString(Context context, String fileName) {
    String str = null;/*from w w  w .  ja va  2  s .  com*/
    try {
        InputStream is = context.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        str = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static InputStream readAssetsFile(Context context, String filePath) {
    InputStream inputStream = null;
    try {// w w  w .  j  av a2s.  c om
        inputStream = context.getAssets().open(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return inputStream;
}

From source file:Main.java

public static String getAssetFileContent(Context context, String assetFileName) {
    AssetManager assetManager = context.getAssets();
    InputStream is = null;/*  w  ww . j  a v  a2s . c o  m*/
    BufferedReader reader = null;
    String lineSeparator = System.getProperty("line.separator");
    try {
        is = assetManager.open(assetFileName);
        reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while (((line = reader.readLine()) != null)) {
            sb.append(line).append(lineSeparator);
        }
        return sb.toString();
    } catch (IOException e) {
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
        }
    }
    return null;
}

From source file:Main.java

public static Typeface getTypface(@Nullable String font_name, Context mContext) {
    if (font_name == null)
        return Typeface.DEFAULT;
    return Typeface.createFromAsset(mContext.getAssets(), "fonts/" + font_name);
}

From source file:Main.java

public static byte[] loadAsset(Context context, String asset) {
    byte[] buffer = null;
    try {/*w ww . ja v a 2  s  . c om*/
        InputStream is = context.getAssets().open(asset);
        int size = is.available();
        buffer = new byte[size];
        is.read(buffer);
        is.close();
    } catch (IOException e) {
        Log.e(TAG, "Failed to load asset " + asset + ": " + e);
    }
    return buffer;
}

From source file:Main.java

public static Typeface getTypeFaceRobotoRegular(Context context) {
    if (typeFaceRobotoRegular == null) {
        typeFaceRobotoRegular = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
    }//from  www. ja  v a  2  s . c  om
    return typeFaceRobotoRegular;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFile) {
    String json = null;/*w  w w .j a v a  2  s. co  m*/
    try {
        InputStream is = context.getAssets().open(jsonFile);
        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;
}