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 String extractAssetToString(Context context, String file) {
    String json;/* w w  w  .ja  v a 2  s. c  o m*/
    try {
        InputStream is = context.getAssets().open(file);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

public static String readFromAsstes(Context context, String fileName) {
    AssetManager assetManager = context.getAssets();
    ByteArrayOutputStream bos = null;
    InputStream inputStream = null;
    String result = null;//from ww  w .ja  va  2s .c  o m
    try {
        inputStream = assetManager.open(fileName, AssetManager.ACCESS_STREAMING);
        bos = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int length;
        while ((length = inputStream.read(data)) != -1) {
            bos.write(data, 0, length);
        }
        result = bos.toString().replaceAll("\\s*", "");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (inputStream != null)
                inputStream.close();
        } catch (IOException ignored) {
        }
    }
    return result;
}

From source file:Main.java

/**
 * Read a file from assets// ww w  .j a v a  2  s .c om
 *
 * @return the string from assets
 */

public static String getfromAssets(Context ctx, String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}

From source file:Main.java

private static void initFonts(Context context) {
    if (robotoRegular == null) {
        robotoRegular = Typeface.createFromAsset(context.getAssets(), "Roboto-Regular.ttf");
    }// w  w w.  j a  v a  2 s . c  o m
    if (robotoMedium == null) {
        robotoMedium = Typeface.createFromAsset(context.getAssets(), "Roboto-Medium.ttf");
    }
}

From source file:Main.java

public static Map<String, List<String>> createDictionary(Context context) {
    try {/*  w  ww . j av a2s  . c  o  m*/
        AssetManager am = context.getAssets();
        InputStream is = am.open(DICTIONARY_FILENAME);
        Scanner reader = new Scanner(is);
        Map<String, List<String>> map = new HashMap<String, List<String>>();

        while (reader.hasNextLine()) {
            String word = reader.nextLine();
            char[] keyArr = word.toCharArray();
            Arrays.sort(keyArr);
            String key = String.copyValueOf(keyArr);

            List<String> wordList = map.get(key);
            if (wordList == null) {
                wordList = new LinkedList<String>();
            }
            wordList.add(word);
            map.put(key, wordList);
        }
        reader.close();
        return map;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap getAssetBitmap(Context context, String path) {
    Bitmap mBitmap = null;/*from  ww w. j a  v  a2 s .  c om*/
    try {
        InputStream is = context.getAssets().open(path);
        mBitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:Main.java

public static JSONObject loadJsonFile(Context context, String fileName) throws IOException, JSONException {
    InputStream is = context.getAssets().open(fileName);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);//from  w  w w .  j  a va  2s  .  c  o  m
    is.close();
    String jsonString = new String(buffer);
    return new JSONObject(jsonString);
}

From source file:Main.java

public static String loadFromAssetsFile(String fname, Context context) {
    String result = null;// w  ww.  j  a va2  s.c o  m
    try {
        InputStream in = context.getAssets().open(fname);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff, "UTF-8");
        result = result.replaceAll("\\r\\n", "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFileName) throws IOException {

    AssetManager manager = context.getAssets();
    InputStream is = manager.open(jsonFileName);

    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);/*from   w  w  w.j  a  v a 2  s.  c  o  m*/
    is.close();

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

From source file:Main.java

public static Properties getConfigProperties(Context context) {
    Properties properties = new Properties();
    try {// ww w.j a  v  a  2  s .co m
        properties.load(context.getAssets().open("config.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties;
}