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

/**
 * Read text file to string/*from   w w w  . j  a v  a  2s  .com*/
 */
public static String readAssetFileToString(Context context, String name) {
    BufferedReader in = null;
    try {
        StringBuilder buf = new StringBuilder();
        InputStream is = context.getAssets().open(name);
        in = new BufferedReader(new InputStreamReader(is));

        String str;
        boolean isFirst = true;
        while ((str = in.readLine()) != null) {
            if (isFirst)
                isFirst = false;
            else
                buf.append('\n');
            buf.append(str);
        }
        return buf.toString();
    } catch (IOException e) {
        Log.e(TAG, "Error opening asset " + name);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing asset " + name);
            }
        }
    }

    return null;
}

From source file:org.cocos2dx.lib.Cocos2dxTypefaces.java

public static Typeface get(Context context, String name) {
    synchronized (cache) {
        if (!cache.containsKey(name)) {
            Typeface t = Typeface.createFromAsset(context.getAssets(), name);
            cache.put(name, t);/*  ww  w.  j  a v  a  2 s .c om*/
        }

        return cache.get(name);
    }
}

From source file:org.xbmc.kore.testhelpers.Utils.java

public static String readFile(Context context, String filename) throws IOException {
    InputStream is = context.getAssets().open(filename);

    int size = is.available();

    byte[] buffer = new byte[size];

    is.read(buffer);//from   w  ww  . ja  va 2s.  c om

    is.close();

    return new String(buffer, "UTF-8");
}

From source file:Main.java

public static void copyAssets(Context context, String assetsName, String destFilePath) throws IOException {
    File file = new File(destFilePath);
    FileOutputStream out = new FileOutputStream(file);

    InputStream in = context.getAssets().open(assetsName);

    byte[] buf = new byte[1024];
    int len;//from   w ww  . ja va 2  s.  c o m
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

private static Bitmap loadBitmapFromAssets(Context context, String texturePath) {
    Bitmap loadedBitmap = null;//from w  w  w.jav  a2s .c o  m
    try {
        InputStream ims = context.getAssets().open(texturePath);
        loadedBitmap = BitmapFactory.decodeStream(ims);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return loadedBitmap;
}

From source file:Main.java

/**
 * Loads given image asset, scaling the image down if it is too big to improve performance.
 * @param context Application context/* w w w .j a v  a 2  s  .  c  o  m*/
 * @param path Path in the assets folder of the image to load
 * @return Loaded image bitmap
 */
public static Bitmap loadImageFromAssets(Context context, String path) {
    try {
        // Open the input stream to the image in assets
        InputStream is = context.getAssets().open(path);

        // Load the image dimensions first so that big images can be scaled down (improves memory usage)
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;
        onlyBoundsOptions.inDither = true;
        BitmapFactory.decodeStream(is, null, onlyBoundsOptions);
        is.close();

        if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
            // There was an error while decoding
            return null;
        }

        // Find the bigger dimension (width, height)
        int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth)
                ? onlyBoundsOptions.outHeight
                : onlyBoundsOptions.outWidth;

        // Calculate the sampling ratio for images that are bigger than the thumbnail size
        double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
        int sampleSize = Integer.highestOneBit((int) Math.floor(ratio));

        // Load the image sampled using the calculated ratio
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = sampleSize;
        bitmapOptions.inDither = true;
        is = context.getAssets().open(path);
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, bitmapOptions);
        is.close();

        return bitmap;
    } catch (IOException e) {
        return null;
    }
}

From source file:de.handtwerk.android.IoHelper.java

public static String readAssetAsString(String pPath, Context pContext) throws IOException {

    AssetManager assMan = pContext.getAssets();
    InputStream is = assMan.open(pPath);
    return StringHelper.readStream(is);
}

From source file:Main.java

public static String getShaderSource(Context context, String sourseName) {
    StringBuffer shaderSource = new StringBuffer();
    try {/*from www . j a  v a  2s . c o m*/
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open(sourseName)));
        String tempStr = null;
        while (null != (tempStr = br.readLine())) {
            shaderSource.append(tempStr);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return shaderSource.toString();
}

From source file:Main.java

public static Typeface getTypeface(Context c, String name) {
    synchronized (typefaces) {
        if (!typefaces.containsKey(name)) {
            try {
                InputStream inputStream = c.getAssets().open(name);
                File file = createFileFromInputStream(inputStream, name);
                if (file == null) {
                    return Typeface.DEFAULT;
                }//from   w w w.j ava2s . com
                Typeface t = Typeface.createFromFile(file);
                typefaces.put(name, t);
            } catch (Exception e) {
                e.printStackTrace();
                return Typeface.DEFAULT;
            }
        }
        return typefaces.get(name);
    }
}

From source file:Main.java

public static String readFile(Context mContext, String file, String code) {
    int len = 0;//from   w ww  .j  ava  2s .c o m
    byte[] buf = null;
    String result = "";
    try {
        InputStream in = mContext.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

        result = new String(buf, code);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}