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:com.android.settings.util.Helpers2.java

public static void get_assetsBinary(String fn, Context c) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {/*from w  w  w . jav a2  s  .c om*/
        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:cn.mdict.utils.IOUtil.java

public static byte[] loadBinFromAsset(AssetManager am, String fileName, boolean checkDocExist) {
    try {/*w  ww .ja  va 2  s  .c o m*/
        InputStream is = null;
        if (checkDocExist) {
            File resFile = new File(MdxEngine.getDocDir() + fileName);
            if (resFile.exists() && resFile.isFile()) {
                is = new FileInputStream(resFile);
            }
        }
        if (is == null)
            is = am.open(fileName);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        if (streamDuplicate(is, buffer, null)) {
            return buffer.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //AssetFileDescriptor afd=am.openFd(fileName);
    return null;
}

From source file:com.android.settings.util.Helpers2.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {//from w w  w.j  a  v a  2s.com
        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, "#!" + Helpers2.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:cn.mdict.utils.IOUtil.java

public static boolean copyAssetToFile(AssetManager assets, String filename, boolean overwrite, String targetDir,
        String newFileName) {/* ww  w  . j a v  a 2 s.  co  m*/

    boolean result = false;
    try {
        final String destfilename = targetDir + "/"
                + ((newFileName != null && newFileName.length() > 0) ? newFileName : filename);
        // Open the source file in your assets directory
        InputStream is = new BufferedInputStream(assets.open(filename));
        result = streamToFile(is, destfilename, overwrite, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:org.protocoderrunner.utils.FileIO.java

/**
 * Read the contents of mContext file in the assets directory indicated by fileName
 *//*from   w  w  w  . j  a  va2  s . c  o  m*/
public static String readFromAssets(Context activity, String fileName) throws IOException {
    AssetManager am = activity.getAssets();
    return read(am.open(fileName));
}

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

public static void testDecode(Context ctx) {
    String url = "up.html";
    AssetManager asset = ctx.getAssets();
    InputStream pinput = null;//from w  w w  .ja va  2  s .  c om
    ByteArrayBuffer buffer = new ByteArrayBuffer(1024 * 8);
    try {
        pinput = asset.open(url);
        int lenth = 0;
        while (lenth != -1) {
            byte[] buf = new byte[2048];
            lenth = pinput.read(buf, 0, buf.length);
            if (lenth != -1) {
                buffer.append(buf, 0, lenth);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] bt = buffer.toByteArray();
    long begin = System.currentTimeMillis();
    String dec = htmlDecode(bt, "up");
    long end = System.currentTimeMillis();
    Log.e("ldx", "use time: " + (end - begin));
    Log.e("ldx", dec);
}

From source file:com.hybris.mobile.Hybris.java

private static void populateCategories() {
    String storeName = SDKSettings.getSettingValue(InternalConstants.KEY_PREF_CATALOG);
    try {// ww  w. j a va 2s.com
        AssetManager am = context.getAssets();
        if (storeName == null || "".equals(storeName)) {
            storeName = "electronics";
        }
        storeName = "categories." + storeName.substring(0, storeName.indexOf("/")) + ".plist";
        InputStream fs = am.open(storeName);
        CategoryManager.reloadCategories(fs);
    } catch (IOException e) {
        LoggingUtils.e(LOG_TAG, "Error opening file \"" + storeName + "\". " + e.getLocalizedMessage(),
                getAppContext());
    }
}

From source file:uk.co.armedpineapple.cth.Files.java

/**
 * Copies an assets/*from w  ww.j  a  v a2 s.c om*/
 *
 * @param ctx
 *          a activityContext
 * @param assetFilename
 *          the filename of the asset
 * @param destination
 *          the destination directory
 * @throws IOException
 *           if the asset cannot be copied
 */
public static void copyAsset(Context ctx, String assetFilename, String destination) throws IOException {
    InputStream in = null;

    try {
        AssetManager assetManager = ctx.getAssets();

        in = assetManager.open(assetFilename);

        String newFileName = destination + "/" + assetFilename;
        File newFile = new File(newFileName);
        File dir = newFile.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }

        Log.i(LOG_TAG, "Copying file [" + assetFilename + "] to [" + newFileName + "]");

        FileUtils.copyInputStreamToFile(in, newFile);

    } finally {
        if (in != null) {
            in.close();
        }
    }

}

From source file:com.jrummyapps.busybox.utils.ZipSigner.java

/**
 * Tool to sign JAR files (including APKs and OTA updates) in a way compatible with the mincrypt verifier, using
 * SHA1 and RSA keys.//from www  . j av a 2  s. co  m
 *
 * @param unsignedZip
 *     The path to the APK, ZIP, JAR to sign
 * @param destination
 *     The output file
 * @return true if successfully signed the file
 */
public static boolean signZip(File unsignedZip, File destination) {
    final AssetManager am = App.getContext().getAssets();
    JarArchiveOutputStream outputJar = null;
    JarFile inputJar = null;

    try {
        X509Certificate publicKey = readPublicKey(am.open(PUBLIC_KEY));
        PrivateKey privateKey = readPrivateKey(am.open(PRIVATE_KEY));

        // Assume the certificate is valid for at least an hour.
        long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;

        inputJar = new JarFile(unsignedZip, false); // Don't verify.
        FileOutputStream stream = new FileOutputStream(destination);
        outputJar = new JarArchiveOutputStream(stream);
        outputJar.setLevel(9);

        // MANIFEST.MF
        Manifest manifest = addDigestsToManifest(inputJar);
        JarArchiveEntry je = new JarArchiveEntry(JarFile.MANIFEST_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        manifest.write(outputJar);

        ZipSignature signature1 = new ZipSignature();
        signature1.initSign(privateKey);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writeSignatureFile(manifest, out);

        // CERT.SF
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        je = new JarArchiveEntry(CERT_SF_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        byte[] sfBytes = writeSignatureFile(manifest, new SignatureOutputStream(outputJar, signature));

        signature1.update(sfBytes);
        byte[] signatureBytes = signature1.sign();

        // CERT.RSA
        je = new JarArchiveEntry(CERT_RSA_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);

        outputJar.write(readContentAsBytes(am.open(TEST_KEY)));
        outputJar.write(signatureBytes);

        copyFiles(manifest, inputJar, outputJar, timestamp);
    } catch (Exception e) {
        Crashlytics.logException(e);
        return false;
    } finally {
        IoUtils.closeQuietly(inputJar);
        IoUtils.closeQuietly(outputJar);
    }
    return true;
}

From source file:org.protocoderrunner.utils.FileIO.java

public static String readAssetFile(Context c, String path) {
    String out = null;//  w  ww  .j  a  v  a 2 s .  c  o m
    AssetManager am = c.getAssets();
    try {
        InputStream in = am.open(path);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int i;
        try {
            i = in.read();
            while (i != -1) {
                buf.write(i);
                i = in.read();
            }
            in.close();
        } catch (IOException ex) {
        }
        out = buf.toString();
    } catch (IOException e) {
        e.printStackTrace();
        MLog.e(TAG, e.toString());
    }
    return out;
}