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:edu.mecc.race2ged.helpers.Utils.java

/**
 * Sets the font of a view to Roboto-Light
 * @param context The context of the activity.
 * @param view The view that will use Roboto-Light
 * @param style The style for the font face. i.e Typeface.BOLD
 *//* w w  w  .  ja v a 2s .  c om*/
public static void setRobotoThin(Context context, View view, int style) {
    if (sRobotoThin == null) {
        sRobotoThin = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf");
    }
    setFont(view, sRobotoThin, style);
}

From source file:Main.java

public static String copyAsset(Context context, String assetName, File dir) {
    try {//  ww  w  .  j  a  v  a2 s.  c  o  m
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File outFile = new File(dir, assetName);
        if (!outFile.exists()) {
            AssetManager assetManager = context.getAssets();
            InputStream in = assetManager.open(assetName);
            OutputStream out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            out.close();
        }
        return outFile.getAbsolutePath();
    } catch (Exception e) {

    }
    return "";
}

From source file:com.github.hobbe.android.openkarotz.util.AssetUtils.java

/**
 * Load a bitmap image from the asset filename.
 * @param context the context/*from  w ww .  j  av a2s .  c  o m*/
 * @param filename the name of the image
 * @return the bitmap or {@code null}
 */
public static Bitmap loadBitmapFromAsset(Context context, String filename) {
    // Log.v(LOG_TAG, "Loading bitmap asset " + filename);

    Bitmap bmp = null;
    InputStream is = null;

    try {
        is = context.getAssets().open(filename);
        bmp = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not load bitmap asset " + filename, e);
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // Ignored
            }
        }
    }

    return bmp;
}

From source file:codepath.watsiapp.utils.Util.java

public static void applyPrimaryFont(Context ctx, PagerSlidingTabStrip view) {
    Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + PRIMARY_FONT);
    view.setTypeface(typeface, Typeface.BOLD);
}

From source file:Main.java

public static Bitmap getBitmapFromAsset(Context ctx, String path, int reqWidth, int reqHeight) {
    if (ctx == null || path == null)
        return null;

    Bitmap bitmap = null;//from w  w  w.  j  av  a 2  s .c o m
    try {
        AssetManager assets = ctx.getAssets();
        InputStream is = assets.open(path);
        bitmap = decodeStream(is, reqWidth, reqHeight);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:com.theaetetuslabs.apkmakertester.ApkMakerService.java

public static void moveAsset(Context context, String assetName, File destDir) throws IOException {
    File dest = new File(destDir, assetName);
    try {//from   ww w . j  a v a 2 s .  c  om
        InputStream assetIn = context.getAssets().open(assetName);
        dest.createNewFile();
        int length = 0;
        byte[] buffer = new byte[4096];
        FileOutputStream rawOut = new FileOutputStream(dest);
        while ((length = assetIn.read(buffer)) > 0) {
            rawOut.write(buffer, 0, length);
        }
        rawOut.close();
        assetIn.close();
    } catch (IOException e) {
        Log.e(TAG, "Could not move " + assetName + "!");
        e.printStackTrace();
        throw new IOException(e);
    }

}

From source file:Main.java

public static Bitmap getSampledBitmap(Context context, String imagePath, boolean isDefault, int reqWidth,
        int reqHeight) {
    Bitmap sampledBitmap = null;//  ww w .j av a2  s  .c o  m
    InputStream inputStream = null;
    try {
        if (isDefault) {
            inputStream = context.getAssets().open(imagePath);
        } else {
            inputStream = new FileInputStream(imagePath);
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream, null, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        if (!isDefault) {
            inputStream.close();
            inputStream = new FileInputStream(imagePath);
        }
        sampledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
        //            inputStream.close();

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

From source file:Main.java

public static String getAssetString(Context ct, String name) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int ch = -1;// ww w.  ja  v a2s  .c om
    byte[] byteData = null;
    InputStream is = null;
    try {
        is = ct.getAssets().open(name);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Read the entire asset into a local byte buffer.  
    try {
        while ((ch = is.read(buf)) != -1) {
            baos.write(buf, 0, ch);//
        }
        byteData = baos.toByteArray();
        baos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String data = null;
    try {
        data = new String(byteData, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return data;
}

From source file:org.acdd.android.initializer.BundleParser.java

public static void parser(Context mContext) {
    InputStream is;//  www  .  ja va2 s.  c  om
    ArrayList<BundleInfoList.BundleInfo> bundleInfos = new ArrayList<BundleInfoList.BundleInfo>();
    try {
        is = mContext.getAssets().open("bundle-info.json");
        int size = is.available();

        // Read the entire asset into a local byte buffer.
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        JSONArray jsonArray = new JSONArray(new String(buffer));
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject tmp = jsonArray.optJSONObject(index);
            BundleInfo mBundleInfo = new BundleInfo();

            mBundleInfo.bundleName = tmp.optString("pkgName");
            mBundleInfo.hasSO = tmp.optBoolean("hasSO");

            ArrayList<String> components = new ArrayList<String>();

            JSONArray activities = tmp.optJSONArray("activities");
            for (int j = 0; j < activities.length(); j++) {
                components.add(activities.getString(j));

            }

            JSONArray receivers = tmp.optJSONArray("receivers");
            for (int j = 0; j < receivers.length(); j++) {
                components.add(receivers.getString(j));

            }

            JSONArray services = tmp.optJSONArray("services");
            for (int j = 0; j < services.length(); j++) {
                components.add(services.getString(j));
            }

            JSONArray contentProviders = tmp.optJSONArray("contentProviders");
            for (int j = 0; j < contentProviders.length(); j++) {
                components.add(contentProviders.getString(j));

            }

            JSONArray dependencys = tmp.optJSONArray("dependency");
            for (int j = 0; j < dependencys.length(); j++) {
                mBundleInfo.DependentBundles.add(dependencys.getString(j));
            }
            mBundleInfo.Components = components;
            bundleInfos.add(mBundleInfo);

        }
        BundleInfoList.getInstance().init(bundleInfos);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:net.henryco.opalette.api.utils.Utils.java

public static String getSourceAssetsText(String file, Context context) {
    try {/*  w  ww  . j a  va 2s.  c  om*/
        InputStream is = context.getAssets().open(file);
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        is.close();
        return new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}