Android Reading File From Assets

To read file from the assets folder in android
we can use:



context.getAssets().open(filename)


Simple Example for reading the JSON file as string from the assets folder




 public static String ReadFileToString(Context context, String filename)
    {

        String json = null;
        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 (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }



Comments