Android Reading File From Assets

sapan
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;
    }



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !