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:Main.java

public static String getStringFromFileInAssets(Context ctx, String filename, boolean useNewline)
        throws IOException {
    InputStream is = ctx.getAssets().open(filename);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder builder = new StringBuilder();
    String line;//ww  w . ja v  a2 s.  c  o  m
    while ((line = reader.readLine()) != null) {
        builder.append(line + (useNewline ? "\n" : ""));
    }
    is.close();
    return builder.toString();
}

From source file:Main.java

public static void copyAssets(Context pContext, String pAssetFilePath, String pDestDirPath) {
    AssetManager assetManager = pContext.getAssets();
    InputStream in = null;/* ww w.  j a  v  a2 s.c o  m*/
    OutputStream out = null;
    try {
        in = assetManager.open(pAssetFilePath);
        File outFile = new File(pDestDirPath, pAssetFilePath);
        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: " + pAssetFilePath, e);
    }
}

From source file:Main.java

public static String readAssetTextFile(Context context, String inFile) {
    String tContents = "";

    try {/*from  w  w  w .ja  v a 2s  .c om*/
        InputStream stream = context.getAssets().open(inFile);

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions here
    }

    return tContents;

}

From source file:Main.java

public static String getFromAssets(Context context, String fileName) {
    try {//  ww  w.ja  va  2  s.  c  o  m
        InputStreamReader inputReader = new InputStreamReader(context.getAssets().open(fileName));
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line = "";
        String Result = "";
        while ((line = bufReader.readLine()) != null) {
            if (line.trim().equals(""))
                continue;
            Result += line + "\r\n";
        }
        return Result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.arasthel.almeribus.utils.ReadData.java

public static void leerParadas(Context context) throws IOException, JSONException {
    InputStream is = context.getAssets().open("paradas.json");
    StringBuilder builder = new StringBuilder();
    Scanner scan = new Scanner(is);
    while (scan.hasNext()) {
        builder.append(scan.nextLine());
    }/*from  w  ww. j av a2 s.c  o  m*/
    scan.close();
    is.close();
    JSONObject json = new JSONObject(builder.toString());
    JSONArray paradas = json.getJSONArray("paradas");
    Parada p;
    JSONObject paradaJson;
    DataStorage.paradas.clear();
    for (int i = 0; i < paradas.length(); i++) {
        paradaJson = paradas.getJSONObject(i);
        String coord = paradaJson.optString("coord");
        if (coord.length() == 0) {
            p = new Parada(paradaJson.getInt("id"), paradaJson.getString("nombre"));
        } else {
            p = new Parada(paradaJson.getInt("id"), paradaJson.getString("nombre"),
                    paradaJson.getString("coord"));
        }
        Log.d("PARADA", p.toString());
        DataStorage.paradas.put(p.getId(), p);
    }
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String path) {
    InputStream ins = null;//  ww  w. j  a  v a 2 s.c om
    Bitmap bitmap = null;
    try {
        ins = context.getAssets().open(path);
        bitmap = BitmapFactory.decodeStream(ins);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

private static InputStream getAssetsStream(Context context, String path) throws IOException {
    return context.getAssets().open(path);
}

From source file:Main.java

public static String readFileAsset(Context ctx, String filename) throws IOException {
    StringBuilder builder = new StringBuilder();

    Scanner s = new Scanner(ctx.getAssets().open(filename));
    while (s.hasNextLine()) {
        builder.append(s.nextLine());/*w w w  . j  a v a  2 s .co m*/
    }
    return builder.toString();
}

From source file:Main.java

public static boolean unzipFile(Context context) {
    try {/*from w w  w . j a v  a  2s  . c  om*/
        InputStream is = context.getAssets().open("tessdata.zip");
        boolean result = unzipFile(is, dstPath(context) + File.separator + "tessdata");
        return result;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String filename) {
    String json = null;//w ww .  j av  a2  s . c  o m
    try {

        InputStream is = context.getAssets().open(filename);

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}