Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get data from assets file in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 735
    Comment on it

    In the below example I have described how to get data from assets folder. Firstly I have created txt.json file in assets folder. In txt.json file I have added JSON data. Now See In MainActivity code I have created loadJSONFromAsset() method, in this method getAssets()method returns an AssetManager instance.

    Step(1)- Created a txt.json file in assets folder and added below JSON data in txt.json file-
     

    {
      "domain":"Java",
      "employee": {
        "name":       "sachin",
        "salary":      76000,
        "married":    true
      }
    }

    Step(2)MainActivity-

    public class MainActivity extends ActionBarActivity {
        TextView text1, text2,text3,text4;
        String json = null;
        public static final String JSON_STRING = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            text1 = (TextView) findViewById(R.id.domain);
    
            text2 = (TextView) findViewById(R.id.emp_name);
            text3 =(TextView)findViewById(R.id.salary);
            text4=(TextView)findViewById(R.id.married);
    
            String json = loadJSONFromAsset();
    
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
    
    
                text1.setText(jsonObject.getString("domain"));
    
    
                  //employee description
                JSONObject empObject = jsonObject.getJSONObject("employee");
    
                //employee name
                text2.setText(empObject.getString("name"));
                //employee salary
                text3.setText(empObject.getString("salary"));
                //employee married status
                text4.setText(empObject.getString("married"));
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
        }
    
        public String loadJSONFromAsset() {
            try {
                InputStream is = getAssets().open("txt.json");
    
                int size =is.available();
                // Read the entire asset into a local byte buffer.
                byte[]buffer= new byte[size];
    
                is.read(buffer);
                is.close();
                json=new String(buffer,"UTF-8");
    
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return json;
        }
    
    
        }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: