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

public static void copyAssets(Context context, File targetDir) {
    AssetManager assetManager = context.getAssets();
    String[] files = null;/*from  www.  j  a va 2s  . co  m*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {

        if (isSupported(filename)) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);

                File outFile = new File(targetDir, filename);

                out = new FileOutputStream(outFile);
                copyFile(in, out);
            } catch (IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close inputstream: " + filename, e);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close outputstream: " + filename, e);
                    }
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Sets a determined font on a button element view
 *
 * @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 buttons Buttons to which the font will be applied
 */// w  w w . j  a v  a2 s  . co  m
public static void setTypeface(Context context, String font, Button... buttons) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    for (Button txt : buttons) {
        txt.setTypeface(tf);
    }
}

From source file:Main.java

public static Typeface get(Context c, String name) {
    synchronized (cache) {
        if (!cache.containsKey(name)) {
            try {
                Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s", name));
                cache.put(name, t);// w ww .ja  va2 s .  com
                return t;
            } catch (RuntimeException e) {
                return null;
            }
        }
        return cache.get(name);
    }
}

From source file:Main.java

/**
 * Loads lines of text from the given path in the assets directory.
 * @param context Application context./*from   w w  w . j ava2 s.  c  om*/
 * @param path Path in the assets folder to the text file to load.
 * @return String array representing lines of text in the file.
 */
public static String[] loadTextFromAssets(Context context, String path) {
    try {
        // Open the input stream to the text in assets
        InputStream inputStream = context.getAssets().open(path);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        List<String> lines = new ArrayList<String>();

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            lines.add(line);
        }

        inputStream.close();

        return lines.toArray(new String[lines.size()]);
    } catch (IOException e) {
        return null;
    }

}

From source file:Main.java

public static void copyFileFromAssets(Context context, String fileName, File outputFile) {
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesCount;
    try {//from ww  w.ja va2s.c  o m
        InputStream imageStream = context.getAssets().open(fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        while ((bytesCount = imageStream.read(buffer)) >= 0) {
            fileOutputStream.write(buffer, 0, bytesCount);
        }
        fileOutputStream.close();
        imageStream.close();
    } catch (IOException | Resources.NotFoundException e) {
        e.printStackTrace();
    }
}

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 textViews TextViews to which the font will be applied
 *///from  w ww .  j  a  v a2  s  . c  om
public static void setTypeface(Context context, String font, TextView... textViews) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    for (TextView txt : textViews) {
        txt.setTypeface(tf);
    }
}

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 group   Root layout in which TextView and Buttons will be searched to apply the font
 *///from  w  ww.  j a v a2 s.  c om
public static void setTypeface(Context context, String font, 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);
        else if (v instanceof ViewGroup)
            setTypeface(context, font, (ViewGroup) v);
    }
}

From source file:Main.java

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 * @param context to work with assets/* ww w .  j  a v a2 s  .  c om*/
 * @param defaultFontNameToOverride for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride,
        String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(),
                customFontFileNameInAssets);

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static Properties getProperties(Context context) {
    Properties props = new Properties();
    //      InputStream in = Utils.class.getResourceAsStream("/gewara.properties");
    try {/*from  w  w w . ja  v a2s  .c  o m*/
        InputStream in = context.getAssets().open("gewara.properties");
        props.load(in);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return props;
}

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}//  w  w w. j  a va 2  s.  c om
 * @param buttons Buttons to which the font will be applied
 */
public static void setTypeface(Context context, String font, int style, Button... buttons) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    for (Button txt : buttons) {
        txt.setTypeface(tf, style);
    }
}