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 openAssetConfigFile(final AssetManager assetManager, final String filename)
        throws IOException {
    final String assetPath = new File(CARDBOARD_CONFIG_FOLDER, filename).getPath();
    return assetManager.open(assetPath);
}

From source file:Main.java

private static void copyFile(AssetManager assetManager, String filename, String destination) {
    InputStream in = null;// w  w  w  .  j  a  v a  2s . c o m
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = destination + File.separator + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }
}

From source file:Main.java

public static String loadAssetsText(AssetManager assetManager, String fileName, String charset) {
    String text = null;//from ww  w  . ja  v a 2s . c om
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(assetManager.open(fileName), charset));
        StringBuilder sb = new StringBuilder();
        while ((text = br.readLine()) != null) {
            sb.append(text);
        }
        text = sb.toString();
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }
    return text;
}

From source file:Main.java

private static String readAssetToStringImpl(AssetManager assetManager, String fileName,
        StringBuilder stringBuilder) throws IOException {

    stringBuilder.setLength(0);/* w  ww. j a va  2 s  .  c o m*/

    InputStream in = assetManager.open(fileName);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    String line;
    while ((line = reader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }

    return stringBuilder.toString();

}

From source file:org.zywx.wbpalmstar.platform.certificates.Http.java

private static InputStream getInputStream(String cPath, Context ctx) throws IOException, FileNotFoundException {
    InputStream inStream;//  w  w  w  . ja v a  2 s.  c  om
    String assertFile = "file:///android_asset/";
    String sdcardFile = "/sdcard/";
    String wgtFile = "widget/";
    String file = "file://";
    if (cPath.contains(assertFile)) {
        cPath = cPath.substring(assertFile.length());
        AssetManager asset = ctx.getAssets();
        inStream = asset.open(cPath);
    } else if (cPath.contains(sdcardFile)) {
        if (cPath.contains(file)) {
            cPath = cPath.substring("file://".length());
        }
        inStream = new FileInputStream(cPath);
    } else if (cPath.startsWith(wgtFile)) {
        AssetManager asset = ctx.getAssets();
        inStream = asset.open(cPath);
    } else {
        inStream = new FileInputStream(cPath);
    }
    return inStream;
}

From source file:Main.java

/**
 *
 *
 * @param context/*w  w w .  jav a2  s .c o  m*/
 * @param assetFilename
 * @param out
 * @throws IOException
 */
public static void copy(Context context, String assetFilename, File out) throws IOException {
    if (context == null) {
        return;
    } else if (out == null) {
        return;
    }

    if (!out.getParentFile().exists()) {
        out.getParentFile().mkdirs();
    }

    AssetManager assetManager = context.getAssets();
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = assetManager.open(assetFilename);
        fos = new FileOutputStream(out);
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = is.read(buf, 0, 1024)) != -1) {
            fos.write(buf, 0, len);
        }
    } finally {
        if (fos != null)
            fos.close();

        if (is != null)
            is.close();
    }
}

From source file:Main.java

public static void modifyByte(Activity activity, String srcFile, String destFile, int byteNum,
        byte byteContents) {
    //        throw new UnsupportedOperationException("Not yet implemented");
    //        UIUtils.log("Modifying byte: " + byteNum + " in Asset file: " + srcFile);
    try {/*  w  ww .  j ava2 s . c  om*/
        AssetManager am = activity.getResources().getAssets(); // get the local asset manager
        InputStream is = am.open(srcFile); // open the input stream for reading
        OutputStream os = new FileOutputStream(destFile);
        byte[] buf = new byte[8092];
        int n;
        int totalBytes = 0;
        while ((n = is.read(buf)) > 0) {
            totalBytes += n;
            //                UIUtils.log("Total Bytes read: " + totalBytes);
            if (byteNum <= totalBytes) {
                //                    UIUtils.log("Replacing byte: " + (totalBytes - n + byteNum ) + " contents with: " + byteContents);
                buf[totalBytes - n + byteNum] = byteContents;
            }
            os.write(buf, 0, n);
        }
        //            UIUtils.log(ByteUtils.ByteArrayToString(buf));
        os.close();
        is.close();
    } catch (Exception ex) {
        Log.e("Installer", "failed to modify file: " + ex);
    }
}

From source file:Main.java

/**
 * Gets properties from Assets/*from www . ja  va 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 boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
    InputStream in = null;//from   w w w  .jav  a 2 s. c  om
    OutputStream out = null;
    try {
        in = assetManager.open(fromAssetPath);
        new File(toPath).createNewFile();
        out = new FileOutputStream(toPath);
        copyFile(in, out);
        in.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Read a file from assets/*  www. j  a  v a 2  s. c  o m*/
 *
 * @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();

}