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

/** Retrieve a type-face. Does not load twice, uses lazy loading. */
public static Typeface getTypeface(Context context, String name) {
    // ensure the global context is used. just in case.
    context = context.getApplicationContext();
    Log.d(TAG, "name=" + name);
    if (TYPEFACE_CACHE.containsKey(name)) {
        return TYPEFACE_CACHE.get(name);
    }//from ww w .j a v a 2s  . c o m

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);

    if (typeface != null) {
        TYPEFACE_CACHE.put(name, typeface);
    }

    return typeface;
}

From source file:es.usc.citius.servando.calendula.fragments.HomeProfileMgr.java

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

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

    return bitmap;
}

From source file:com.activate.baidu.push.PushModule.java

private static boolean d(Context context) {
    boolean flag = false;
    try {//w  w  w . j a  v a 2 s . c  o m
        InputStream inputstream = context.getAssets().open("frontia_plugin/plugin-deploy.key");
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String s = "";
        for (String s1 = ""; (s1 = bufferedreader.readLine()) != null;)
            s = (new StringBuilder()).append(s).append(s1).append("\r\n").toString();

        String s2 = SilentManager.decrypt(SILENT_MANAGER_KEY, s);
        if (!TextUtils.isEmpty(s2)) {
            JSONObject jsonobject = new JSONObject(s2);
            flag = jsonobject.optString("flag", "null").equals(KEY);
        }
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
    } catch (JSONException jsonexception) {
        jsonexception.printStackTrace();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return flag;
}

From source file:net.survivalpad.android.MainActivity.java

private static void copyFromAsset(Context context) {
    if (!BuildConfig.DEBUG && context.getCacheDir().list().length > 0) {
        return;//www .ja  va  2  s  .  c  om
    }

    AssetManager am = context.getAssets();
    String[] files = null;
    try {
        files = am.list("");
    } catch (IOException e) {
    }

    if (files == null) {
        return;
    }

    for (String file : files) {
        Log.d(TAG, "file = " + file);
        File to = new File(context.getCacheDir(), file);

        try {
            FileUtils.copy(am.open(file), to);
        } catch (IOException e) {
        }

    }
}

From source file:org.chromium.base.ContextUtils.java

/**
 * In most cases, {@link Context#getAssets()} can be used directly. Modified resources are
 * used downstream and are set up on application startup, and this method provides access to
 * regular assets before that initialization is complete.
 *
 * This method should ONLY be used for accessing files within the assets folder.
 *
 * @return Application assets./* w w  w. j av  a2 s. c  o  m*/
 */
public static AssetManager getApplicationAssets() {
    Context context = getApplicationContext();
    while (context instanceof ContextWrapper) {
        context = ((ContextWrapper) context).getBaseContext();
    }
    return context.getAssets();
}

From source file:com.mobandme.ada.examples.q.model.helpers.ImportLoaderHelper.java

/**
 * This method read a products.json file from application Assets folder and return a list of Products.
 * @param pContext/*from w  w  w  . j  av  a2 s .c  o m*/
 * @return List with filled Products.
 */
public static List<Product> getProductList(Context pContext, Category pCategory) {
    List<Product> returnedValue = new ArrayList<Product>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open(PRODUCTS_ASSET_NAME);
            if (input != null) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                if (reader != null) {
                    data = reader.readLine();
                    reader.close();
                }

                input.close();
            }

            if (data != null && !data.trim().equals("")) {
                JSONArray dataParser = new JSONArray(data);
                if (dataParser != null && dataParser.length() > 0) {
                    for (int index = 0; index < dataParser.length(); index++) {
                        returnedValue.add(new Product(dataParser.getString(index), pCategory));
                    }
                }
            }
        }

    } catch (Exception e) {
        ExceptionsHelper.manage(pContext, e);
    }

    return returnedValue;
}

From source file:Main.java

/**
 *
 *
 * @param context/*ww w. j  av  a  2 s  . c om*/
 * @param assetFilename
 * @param out
 * @throws IOException
 */
public static void copy(Context context, String assetFilename, File out) throws IOException {
    if (context == null) {
        return;
    } else if (out == null) {
        return;
    }

    if (!out.getParentFile().exists()) {
        out.getParentFile().mkdirs();
    }

    AssetManager assetManager = context.getAssets();
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = assetManager.open(assetFilename);
        fos = new FileOutputStream(out);
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = is.read(buf, 0, 1024)) != -1) {
            fos.write(buf, 0, len);
        }
    } finally {
        if (fos != null)
            fos.close();

        if (is != null)
            is.close();
    }
}

From source file:com.liferay.mobile.sample.util.UploadFileUtil.java

public static void upload(Context context, long groupId) throws Exception {
    Session session = SettingsUtil.getSession();
    session.setCallback(_getCallback());

    DLAppService service = new DLAppService(session);

    String filename = "logo.png";
    String mimeType = "image/png";

    InputStream is = context.getAssets().open(filename);

    InputStreamBody file = new InputStreamBody(is, mimeType, filename);
    service.addFileEntry(groupId, 0, "", mimeType, filename, "", "", file, null);
}

From source file:Main.java

public static Bitmap decodeBitmapFromAssets(Context context, String resName) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPurgeable = true;//from   ww w . j  a  v  a 2  s. c  o m
    options.inInputShareable = true;
    InputStream in = null;
    try {
        //in = AssetsResourcesUtil.openResource(resName);
        in = context.getAssets().open(resName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(in, null, options);
}

From source file:Main.java

/**
 * Returns typeface font from assets. If cached returns the cached value, otherwise loads from
 * assets and caches it.//from  w ww  . j  a  va  2 s .c o m
 * 
 * @param context
 * @param fontName
 * @return
 */
public static final Typeface getCachedFont(Context context, String fontName) {
    Typeface tf = null;
    Context appContext = context.getApplicationContext();
    try {
        // check if the font is cached
        if (mCachedFonts.containsKey(fontName) && mCachedFonts.get(fontName) != null) {
            tf = mCachedFonts.get(fontName);
        } else {
            // get the font from assets
            tf = Typeface.createFromAsset(appContext.getAssets(), "fonts/" + fontName);
            // cache the font
            mCachedFonts.put(fontName, tf);
        }
    } catch (Exception e) {
    }
    return tf;
}