Example usage for android.content.res Resources getAssets

List of usage examples for android.content.res Resources getAssets

Introduction

In this page you can find the example usage for android.content.res Resources getAssets.

Prototype

public final AssetManager getAssets() 

Source Link

Document

Retrieve underlying AssetManager storage for these resources.

Usage

From source file:Main.java

public static InputStream readSampleJson(Resources resources) {
    try {/*from   w  w  w .  ja  va  2 s.  c  o m*/
        return resources.getAssets().open("sample.json");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Load an asset from a resource and return the content as byte array.
 *//*from   w w w.  j  a va  2 s  . c  o  m*/
public static byte[] assetAsByteArray(Resources res, String path) throws IOException {
    InputStream is = res.getAssets().open(path);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    byte[] temp = new byte[1024];
    int read;

    while ((read = is.read(temp)) > 0) {
        buf.write(temp, 0, read);
    }
    is.close();
    return buf.toByteArray();
}

From source file:Main.java

public static String getStringFromFile(Resources resources, String filePath) {
    try {/*from  w  w  w  . j  a v a  2  s. c  om*/
        InputStream is = resources.getAssets().open(filePath);
        String ret = convertStreamToString(is);
        is.close();
        return ret;
    } catch (IOException e) {
        throw new RuntimeException("unable to load asset " + filePath);
    }
}

From source file:Main.java

/** Read a properties file from /assets.  Returns null if it does not exist. */
public static Properties getProperties(String name, Context context) {
    Resources resources = context.getResources();
    AssetManager assetManager = resources.getAssets();

    // Read from the /assets directory
    try {/*w ww .  ja  v  a  2s.com*/
        InputStream inputStream = assetManager.open(name);
        Properties properties = new Properties();
        properties.load(inputStream);
        return properties;
    } catch (IOException e) {
        Log.i("ChatSecure", "no chatsecure.properties available");
        return null;
    }
}

From source file:Main.java

/**
 * Gets properties from Assets//  w ww . j ava  2  s  .c  o  m
 *
 * @param name    Properties file
 * @param context Context
 * @return Properties
 */
public static Properties getProperties(String name, Context context) {
    Resources resources = context.getResources();
    AssetManager assetManager = resources.getAssets();
    Properties properties = null;

    // Read from the /assets directory
    try {
        InputStream inputStream = assetManager.open(name);
        properties = new Properties();
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties;
}

From source file:Main.java

public static String getStringFromAssets(String strFileName, Resources resources) {
    String result = null;/*from ww w. j  a  v  a2 s  . com*/
    try {
        InputStream in = resources.getAssets().open(strFileName);
        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);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

private static void loadUAO(Resources res) throws Exception {
    InputStream in = res.getAssets().open("big5uao");
    big5_to_ucs = new char[Big5TSize];
    byte r[] = new byte[2];
    for (short i = 0; i < big5_to_ucs.length; i++) {
        if (in.read(r) != -1)
            /* albb.100618: byte is signed, however char is not */
            big5_to_ucs[i] = (char) (((char) r[0] & 0xFF) << 8 | (char) r[1] & 0xFF);
        else//w  w w. j a  v a2  s.  c  o  m
            return;
    }
}

From source file:Main.java

static private void resetLocale(Resources res) {
    Configuration conf = new Configuration(res.getConfiguration());
    conf.locale = mCurLocale;//from  w  ww. j a  v  a 2 s  . c  o  m
    new Resources(res.getAssets(), res.getDisplayMetrics(), conf);
}

From source file:Main.java

public static Bitmap getbmFromAssetsFile(Resources res, String fileName) {
    if (res == null)
        return null;
    Bitmap bm = null;//from w w w.j ava  2  s .  c om
    AssetManager am = res.getAssets();
    try {
        InputStream is = am.open(fileName);
        bm = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bm;

}

From source file:net.bible.service.common.FileManager.java

public static Properties readPropertiesFile(String folder, String filename) {
    Properties returnProperties = new Properties();

    Resources resources = BibleApplication.getApplication().getResources();
    AssetManager assetManager = resources.getAssets();
    if (!filename.endsWith(DOT_PROPERTIES)) {
        filename = filename + DOT_PROPERTIES;
    }//w  ww. j a  va  2  s . c o  m
    if (StringUtils.isNotEmpty(folder)) {
        filename = folder + File.separator + filename;
    }

    // Read from the /assets directory
    InputStream inputStream = null;
    try {
        // check to see if a user has created his own reading plan with this name
        inputStream = assetManager.open(filename);

        returnProperties.load(inputStream);
        log.debug("The properties are now loaded from: " + filename);
    } catch (IOException e) {
        System.err.println("Failed to open property file:" + filename);
        e.printStackTrace();
    } finally {
        IOUtil.close(inputStream);
    }
    return returnProperties;
}