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

static List<String> getJsonAssets(Context context, String path) throws IOException {
    String[] assetList = context.getAssets().list(path);
    List<String> files = new ArrayList<>();
    for (String asset : assetList) {
        if (asset.toLowerCase().endsWith(".json")) {
            files.add(asset);//from w  w  w  .  j a v  a  2s.co  m
        }
    }
    return files;
}

From source file:Main.java

public static Bitmap getBitmapFromAsset(Context context, String strName) {
    AssetManager assetManager = context.getAssets();

    InputStream istr;//w  w w.jav  a 2s  .  c  om
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(strName);
        bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        return null;
    }

    return bitmap;
}

From source file:Main.java

private static Bitmap getBitmapFromAsset(Context context, String filePath) {
    AssetManager assetManager = context.getAssets();

    InputStream istream;//from ww  w  .  j  av a 2 s.  c o m
    Bitmap bitmap = null;
    try {
        istream = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(istream);
    } catch (IOException e) {
        // handle exception
    }

    return bitmap;
}

From source file:Main.java

public static String getJsonAsString(String filename, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    StringBuilder buf = new StringBuilder();
    InputStream json = manager.open(filename);
    BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));
    String str;/*from   w ww  . j  a  v a 2 s  .  c  o  m*/

    while ((str = in.readLine()) != null) {
        buf.append(str);
    }

    in.close();

    return buf.toString();
}

From source file:Main.java

/**
 * //from   w  w  w. j a  va  2  s . c  o m
 * @param context
 * @param assetsPah:"fonts/xxx.ttf"
 * @return
 * usage:textView.setTypeface(typeface);
 */
public static Typeface getTypeface(Context context, String assetsPah) {
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), assetsPah);
    return typeface;
}

From source file:Main.java

public static AssetManager getAssetManager(Context context) {
    if (mAssetManager == null) {
        mAssetManager = context.getAssets();
    }/*  w  w w  .  j a va 2s .c  o m*/
    return mAssetManager;
}

From source file:Main.java

public static void setFont(Context context, View view) {
    ((TextView) view).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/"));
}

From source file:Main.java

public static void setFengGe(Context context, TextView tv, String ttf) {
    Typeface fontFace = Typeface.createFromAsset(context.getAssets(), ttf);
    tv.setTypeface(fontFace);//from www. j  ava 2s  . co  m
}

From source file:Main.java

/**
 * Get file content by filename//ww  w .  j a va 2 s .  com
 * 
 * @param c
 * @param filename
 * @return content String
 */
public static String getFileContent(Context c, String filename) {
    try {
        InputStream fin = c.getAssets().open(filename);
        byte[] buffer = new byte[fin.available()];
        fin.read(buffer);
        fin.close();
        return new String(buffer);
    } catch (IOException e) {
        Log.e("inspector", e.getLocalizedMessage());
    }
    return "";
}

From source file:Main.java

/**
 * Parcourt les fichiers dans /assets/ et renvoie la liste des fichiers dont leurs noms est
 * de la forme suivante : XXX____gallery_borneathXXX X pouvant etre une chaine de caracteres
 * vide/*  ww  w.  ja v  a 2  s .c o m*/
 * @param context utilisee
 * @return liste de chaine de caracteres (nom d'image)
 * @throws IOException
 */
public static List<String> getImagesPathFromAssets(Context context) throws IOException {
    AssetManager assetManager = context.getAssets();
    String[] files = assetManager.list(ASSETS_IMAGES_FOLDER);
    List<String> sortedFileNames = new ArrayList<>();
    for (String fileName : files) {
        if (fileName.contains(ASSETS_PATH_NAME_PATTERN)) {
            sortedFileNames.add(ASSETS_IMAGES_FOLDER + FILE_SEPARATOR + fileName);
        }
    }
    return sortedFileNames;
}