I need to read a json file from the SD card and display the data in the spinner. Is there any way to read the data from file in Android and display the contents of that file in spinner?
Cross-platform to open JSON files: Generally, users can open the JSON file in any text editor as it is a plain text-based file. The Google Chrome and Mozilla Firefox web browsers are cross-platform to open JSON files that are compatible with every operating system (OS).
First read the file from SD card and then parse that file
Step-1 Retrieve the data from file from SD card. see tutorial
Step-2 Parse the data. See How to parse JSON String
Sample code
try {
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
FileInputStream stream = new FileInputStream(yourFile);
String jString = null;
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
jString = Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
JSONObject jObject = new JSONObject(jString);
} catch (Exception e) {e.printStackTrace();}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With