Example usage for android.content.res AssetManager open

List of usage examples for android.content.res AssetManager open

Introduction

In this page you can find the example usage for android.content.res AssetManager open.

Prototype

public @NonNull InputStream open(@NonNull String fileName) throws IOException 

Source Link

Document

Open an asset using ACCESS_STREAMING mode.

Usage

From source file:Main.java

public static InputStream openAssetFile(Context context, String file) throws IOException {
    AssetManager assetManager = context.getAssets();

    return assetManager.open(file);

}

From source file:Main.java

public static String AssetJSONFile(String filename, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(filename);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);// w w w .j a  v  a2s.c  o m
    file.close();

    return new String(formArray);
}

From source file:Main.java

public static Drawable loadImage(String name, AssetManager assetManager) {
    try {//from  ww  w . j a v  a 2  s  .  c  om
        InputStream ims = assetManager.open(name);
        return Drawable.createFromStream(ims, null);
    } catch (IOException ex) {
        //throw new RuntimeException( "Image " + name + " could not be loaded" );
        return null;
    }
}

From source file:Main.java

public static byte[] readFileFromAsset(String fileName) throws IOException {
    AssetManager assetMgr = context.getAssets();
    InputStream is = assetMgr.open(fileName);
    int size = is.available();
    byte[] content = new byte[size];
    is.read(content);/*from   w  w  w.j a v  a 2s.co m*/
    is.close();
    return content;
}

From source file:Main.java

public static String readAsset(AssetManager assets, String path) throws IOException {
    InputStream is = assets.open(path);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);//from ww w .j a v a  2 s  .co  m
    is.close();
    return new String(buffer);
}

From source file:Main.java

public static String getProperty(String key, Context context) throws IOException {
    Properties properties = new Properties();
    AssetManager assetManager = context.getAssets();
    InputStream inputStream = assetManager.open("marvel.properties");
    properties.load(inputStream);//w w w. j  ava  2  s.  c om
    return properties.getProperty(key);
}

From source file:Main.java

public static boolean hasAssetFile(Context c, String filePath) {
    boolean hasFile = false;
    InputStream inputStream = null;
    try {/*from   w ww  . java  2 s  .co m*/
        AssetManager am = c.getAssets();
        inputStream = am.open(filePath);
        hasFile = true;
    } catch (Exception e) {
        e.printStackTrace();
        hasFile = false;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return hasFile;
}

From source file:Main.java

/**
 * get bitmap from assert//from www . j av  a2  s .  c  om
 * 
 * @param context
 * @param fileName
 * @return
 */
public static Bitmap getImageFromAssetFile(Context context, String fileName) {
    Bitmap image = null;
    try {
        AssetManager am = context.getAssets();
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {

    }
    return image;
}

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);/*  w  ww.  j a v a 2s .c  o m*/
    is.close();

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

From source file:Main.java

public static Bitmap getImageFromAssetsFile(Context ct, String fileName) {
    Bitmap image = null;/*from   w  w w .  ja  va2s.  c om*/
    AssetManager am = ct.getAssets();
    try {
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;

}