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 String readTextFromFile(Context context, String filePath) throws IOException {
    StringBuilder builder = new StringBuilder();

    AssetManager manager = context.getAssets();
    InputStream stream = manager.open(filePath.replace(ASSET_FOLDER_PREFIX, ""));
    InputStreamReader streamReader = new InputStreamReader(stream);
    BufferedReader bufferedReader = new BufferedReader(streamReader);
    String line;/*from   w  w  w.  j  a  v  a  2 s. c  o m*/
    while ((line = bufferedReader.readLine()) != null) {
        builder.append(line);
        builder.append('\n');
    }
    bufferedReader.close();

    return builder.toString();
}

From source file:Main.java

/**
 * Get file from assets// ww  w.  ja  va  2  s.  c  o m
 *
 * @param context
 * @param fileName
 * @return
 * @throws IOException
 */
public static InputStream getFileFromAssets(Context context, String fileName) throws IOException {
    AssetManager am = context.getAssets();
    return am.open(fileName);
}

From source file:Main.java

public static Bitmap getEmotion(Context context, String key) {
    String fileName = "default_emotions_package/" + MAP.get(key);
    try {//  ww w  .  j  ava2  s .c  o  m
        AssetManager am = context.getAssets();
        return BitmapFactory.decodeStream(am.open(fileName));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    return null;
}

From source file:Main.java

public static Bitmap getbmFromAssetsFile(Resources res, String fileName) {
    if (res == null)
        return null;
    Bitmap bm = null;//  ww w .  java 2  s.  c  o m
    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:Main.java

public static String extractFromAssets(Context ctx, String file, String destinationDirectory)
        throws IOException, FileNotFoundException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    AssetManager assetManager = ctx.getAssets();
    InputStream in = assetManager.open(file);
    String destinationFilename = destinationDirectory + File.separator + file;
    OutputStream out = new FileOutputStream(destinationFilename);
    byte[] buffer = new byte[1024];
    int read;/*from  w  ww . j a v  a  2 s .c  o  m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    out.close();
    return destinationFilename;
}

From source file:Main.java

public static String readAsset(Context context, String assetPath) throws IOException {
    String asset = null;/*from w  ww.j  a v a2s  .  co  m*/
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(assetPath);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        is.close();
        asset = new String(data, "ASCII");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return asset;
}

From source file:Main.java

public static String AssetJSONFile(String filename, AssetManager manager) throws IOException {
    String json = null;//from w ww.  j av  a2 s  . co m
    InputStream is = manager.open(filename);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    json = new String(buffer, "UTF-8");
    return json;
}

From source file:Main.java

public static byte[] loadIntoBytes(String fileName) {
    AssetManager assetManager = context.getAssets();
    try {/*ww w . j  av a  2  s .  c om*/
        InputStream is = assetManager.open(fileName);
        byte buf[] = new byte[is.available()];
        is.read(buf);
        return buf;
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

public static InputStream openAssetFile(Context context, String fileName) {
    AssetManager am = context.getAssets();
    InputStream is = null;/*from   ww  w.  jav  a 2s . co  m*/
    try {
        is = am.open(fileName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return is;
}

From source file:Main.java

public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
    Bitmap image = null;/*from  w w  w. j a v  a2s .  co m*/
    AssetManager am = context.getResources().getAssets();
    try {
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}