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 getProperty(Context context, String key) {
    try {/*from  w  w w.  j a va2  s . co m*/
        Properties props = new Properties();
        InputStream input = context.getAssets().open("config.properties");
        if (input != null) {
            props.load(input);
            return props.getProperty(key);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Read a text file from assets//from   w  ww  . j a  v  a2s . c om
 * 
 * @param context
 * @param fileName
 * @return
 */
public static String read(Context context, String fileName) {
    try {
        AssetManager am = context.getAssets();
        InputStream in = am.open(fileName);
        // String pckg = context.getPackageName();
        // ContentResolver cr = context.getContentResolver();
        // Uri uri = Uri.parse("android.resource://"+pckg+"/"+fileName);
        // InputStream in = cr.openInputStream(uri);
        String txt = read(new InputStreamReader(in));
        return txt;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String readFromAssets(Context context, String fileName) throws IOException {

    InputStream inputStream = context.getAssets().open(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;/*from  w  w w .  ja v  a 2 s . c  o m*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
}

From source file:Main.java

public static String assetFile2Str(Context c, String urlStr) {
    InputStream in = null;//w ww .ja  v a  2  s . c  o m
    try {
        in = c.getAssets().open(urlStr);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        StringBuilder sb = new StringBuilder();
        do {
            line = bufferedReader.readLine();
            if (line != null && !line.matches("^\\s*\\/\\/.*")) {
                sb.append(line);
            }
        } while (line != null);

        bufferedReader.close();
        in.close();

        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:Main.java

private static String[] getAssetsImageNameArray(Context context, String folderName) {
    String[] imageNameArray = null;
    try {//from   ww w  . j  av  a  2  s .  c  om
        imageNameArray = context.getAssets().list(folderName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageNameArray;
}

From source file:Main.java

public static JSONObject getAssetsJson(Context context, String jsonName) {
    try {/*from  w  w  w  .j ava 2  s  .com*/
        InputStreamReader inputStreamReader = new InputStreamReader(context.getAssets().open(jsonName),
                "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        inputStreamReader.close();
        return new JSONObject(stringBuilder.toString());
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String loadJSONFromAsset(Context context, String pathNameFile) {
    String output = null;// w w w  .  j a  v  a 2 s  . c o m
    try {
        InputStream is = context.getAssets().open(DIR_BOARDS_ASSETS + "/" + pathNameFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return output;
}

From source file:Main.java

public static void copyFolder(Context context, String assetPath, String sdcardPath) {
    try {//  www  . j  a  v a2s.co  m
        String[] list = context.getAssets().list(assetPath);

        File f = new File(sdcardPath);
        if (!f.exists())
            f.mkdirs();

        for (String s : list)
            copyFile(context, assetPath + "/" + s, sdcardPath + "/" + s);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * The method read XML from a file, skips irrelevant chars and serves back the content as string. 
 * @param inputStream/*from w w w  .j  ava 2  s  .c o m*/
 * @return String : content of a file 
 * @throws IOException
 */
public static String getFileToString(Context context, String xml_file) throws IOException {
    InputStream inputStream = context.getAssets().open(xml_file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {
        int i = inputStream.read();
        while (i != -1) {
            if (i != TAB && i != NL && i != CR)
                byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        return byteArrayOutputStream.toString("UTF-8");

    } catch (IOException e) {
        e.printStackTrace();
        return "";

    } finally {
        inputStream.close();
    }
}

From source file:Main.java

public static String getApplicationKey(Context context) {
    try {//from   ww  w  . j a va 2 s.  c  o m
        BufferedReader assetsFile = new BufferedReader(
                new InputStreamReader(context.getAssets().open(YUMMLY_CREDS_FILE_NAME)));
        assetsFile.readLine();
        return assetsFile.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }

    throw new IllegalStateException(YUMMLY_CREDS_FILE_NAME
            + " needs to have application ID on first line and application Key on second line");
}