Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

In this page you can find the example usage for android.content Context getAssets.

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

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

public static void copyFileOrDir(Context c, String path) {
    AssetManager assetManager = c.getAssets();
    String assets[] = null;//from w w w . j  a v a2 s . c  o m
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(c, path);
        } else {
            String fullPath = ProjectManager.getInstance().getBaseDir() + path;
            File dir = new File(fullPath);
            if (!dir.exists()) {
                dir.mkdir();
            }
            for (String asset : assets) {
                copyFileOrDir(c, path + "/" + asset);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}

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  v a2  s . co m
public static String readFromAssets(Context activity, String fileName) throws IOException {
    AssetManager am = activity.getAssets();
    return read(am.open(fileName));
}

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

private static void copyFile(Context c, String filename) {
    AssetManager assetManager = c.getAssets();

    InputStream in = null;//ww  w  .j a  v a2s . c o  m
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = ProjectManager.getInstance().getBaseDir() + 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:com.grarak.kerneladiutor.utils.Utils.java

public static String readAssetFile(Context context, String file) {
    InputStream input = null;/*from  w w  w .ja v a  2  s .com*/
    BufferedReader buf = null;
    try {
        StringBuilder s = new StringBuilder();
        input = context.getAssets().open(file);
        buf = new BufferedReader(new InputStreamReader(input));

        String str;
        while ((str = buf.readLine()) != null) {
            s.append(str).append("\n");
        }
        return s.toString().trim();
    } catch (IOException e) {
        Log.e(TAG, "Unable to read " + file);
    } finally {
        try {
            if (input != null)
                input.close();
            if (buf != null)
                buf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

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

public static String readAssetFile(Context c, String path) {
    String out = null;/*ww  w . j a v  a 2s . com*/
    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;
}

From source file:com.cryart.sabbathschool.view.SSReadingView.java

public static String readFileFromAssets(Context context, String assetPath) {
    StringBuilder buf = new StringBuilder();
    try {//from w  w  w. ja v a  2  s . c  o m
        InputStream json = context.getAssets().open(assetPath);
        BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));
        String str;
        while ((str = in.readLine()) != null) {
            buf.append(str);
        }
        in.close();
        return buf.toString();

    } catch (IOException e) {
        return "";
    }
}

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

private static void listAssetsInternal(Context ctx, String path, ArrayList<String> paths) throws IOException {
    AssetManager assetManager = ctx.getAssets();
    String assets[];/* w  w w. j av a  2s. c  o m*/

    assets = assetManager.list(path);

    if (assets.length == 0) {
        paths.add(path);

    } else {
        for (String asset : assets) {
            listAssetsInternal(ctx, path + "/" + asset, paths);
        }
    }

}

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

/**
 * Copies an assets/*from   ww  w. j  av  a 2s.  co  m*/
 *
 * @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:biz.bokhorst.xprivacy.Util.java

private static PublicKey getPublicKey(Context context) throws Throwable {
    // Read public key
    String sPublicKey = "";
    InputStreamReader isr = new InputStreamReader(context.getAssets().open("XPrivacy_public_key.txt"), "UTF-8");
    BufferedReader br = new BufferedReader(isr);
    String line = br.readLine();/*w  ww .j a v a2  s  .  c  om*/
    while (line != null) {
        if (!line.startsWith("-----"))
            sPublicKey += line;
        line = br.readLine();
    }
    br.close();
    isr.close();

    // Create public key
    byte[] bPublicKey = Base64.decode(sPublicKey, Base64.NO_WRAP);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec encodedPubKeySpec = new X509EncodedKeySpec(bPublicKey);
    return keyFactory.generatePublic(encodedPubKeySpec);
}

From source file:org.thoughtland.xlocation.Util.java

private static PublicKey getPublicKey(Context context) throws Throwable {
    // Read public key
    String sPublicKey = "";
    InputStreamReader isr = new InputStreamReader(context.getAssets().open("XLocation_public_key.txt"),
            "UTF-8");
    BufferedReader br = new BufferedReader(isr);
    String line = br.readLine();//from  ww  w .  java2 s.co m
    while (line != null) {
        if (!line.startsWith("-----"))
            sPublicKey += line;
        line = br.readLine();
    }
    br.close();
    isr.close();

    // Create public key
    byte[] bPublicKey = Base64.decode(sPublicKey, Base64.NO_WRAP);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec encodedPubKeySpec = new X509EncodedKeySpec(bPublicKey);
    return keyFactory.generatePublic(encodedPubKeySpec);
}