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 void copyAssets(Context context, File targetDir) {
    AssetManager assetManager = context.getAssets();
    String[] files = null;//ww  w .  j  av  a2s  . co  m
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {

        if (isSupported(filename)) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);

                File outFile = new File(targetDir, filename);

                out = new FileOutputStream(outFile);
                copyFile(in, out);
            } catch (IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close inputstream: " + filename, e);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close outputstream: " + filename, e);
                    }
                }
            }
        }
    }
}

From source file:com.brewcrewfoo.performance.util.Helpers.java

public synchronized static void get_assetsBinary(String fn, Context c) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {//from w w  w  . j av  a2 s .  com
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(buffer);
            fos.close();
        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }
    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}

From source file:com.bellman.bible.service.common.FileManager.java

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

    Resources resources = CurrentActivityHolder.getInstance().getApplication().getResources();
    AssetManager assetManager = resources.getAssets();
    if (!filename.endsWith(DOT_PROPERTIES)) {
        filename = filename + DOT_PROPERTIES;
    }/*from w  w  w. jav a2 s  .  co 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;
}

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;
    }/*from   w  ww . j  av  a2s.  c om*/
    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;
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static String copyFileToStorage(Context context, String inFilePath) {
    if (!BUtility.sdCardIsWork()) {
        return null;
    }//from w w w. j  a  va2  s.c om
    String ext = Environment.getExternalStorageDirectory() + File.separator + "download";
    String fileName = subFileName(inFilePath);
    String newFilePath = ext + File.separator + fileName;
    File file = new File(newFilePath);
    if (file.exists()) {
        return newFilePath;
    }
    try {
        AssetManager assrt = context.getAssets();
        InputStream input = assrt.open(inFilePath);
        file.createNewFile();
        FileOutputStream output = new FileOutputStream(file);
        byte[] temp = new byte[8 * 1024];
        int i = 0;
        while ((i = input.read(temp)) > 0) {
            output.write(temp, 0, i);
        }
        output.close();
        input.close();
    } catch (Exception e) {
        return null;
    }
    return newFilePath;
}

From source file:Main.java

private static void addFolderEntriesToManifest(ArrayList<String> manifestEntries, String path,
        AssetManager assetManager) throws IOException, NoSuchAlgorithmException {
    String[] fileList = assetManager.list(path);
    if (fileList.length > 0) {
        // This is a folder, recursively add folder entries to the manifest.
        for (String pathInFolder : fileList) {
            addFolderEntriesToManifest(manifestEntries, path + "/" + pathInFolder, assetManager);
        }//from   w w w. ja  va 2 s  . co m
    } else {
        // This is a file, compute a hash and create a manifest entry for it.
        manifestEntries.add(path + ":" + computeHash(assetManager.open(path)));
    }
}

From source file:com.brewcrewfoo.performance.util.Helpers.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {/*from w ww . j av a  2 s  .  c  o  m*/
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        final String s = new String(buffer);
        final StringBuilder sb = new StringBuilder(s);
        if (!postfix.equals("")) {
            sb.append("\n\n").append(postfix);
        }
        if (!prefix.equals("")) {
            sb.insert(0, prefix + "\n");
        }
        sb.insert(0, "#!" + Helpers.binExist("sh") + "\n\n");
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(sb.toString().getBytes());
            fos.close();

        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }
    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}

From source file:net.survivalpad.android.MainActivity.java

private static void copyFromAsset(Context context) {
    if (!BuildConfig.DEBUG && context.getCacheDir().list().length > 0) {
        return;//  w  ww . j  a v  a 2  s  . c  om
    }

    AssetManager am = context.getAssets();
    String[] files = null;
    try {
        files = am.list("");
    } catch (IOException e) {
    }

    if (files == null) {
        return;
    }

    for (String file : files) {
        Log.d(TAG, "file = " + file);
        File to = new File(context.getCacheDir(), file);

        try {
            FileUtils.copy(am.open(file), to);
        } catch (IOException e) {
        }

    }
}

From source file:most.voip.api.Utils.java

static void copyAssets(Context ctx) {
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;/*from w  w w  . ja v a2 s.  co  m*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(ctx.getExternalFilesDir(null), filename);
            Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:mx.klozz.xperience.tweaker.helpers.Helpers.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {/*from  w ww  .  j  a v a2s .c o  m*/
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        final String s = new String(buffer);
        final StringBuilder sb = new StringBuilder(s);
        if (!postfix.equals("")) {
            sb.append("\n\n").append(postfix);
        }
        if (!prefix.equals("")) {
            sb.insert(0, prefix + "\n");
        }
        sb.insert(0, "#!" + Helpers.BinExist("sh") + "\n\n");
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(sb.toString().getBytes());
            fos.close();

        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }
    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}