Reputation: 1329
I am facing issue in how to read the image from the server and save the image in the arraylist . The json data from server is given below
{
"DS": {
"LST": [
{
"OID": 1,
"OCD": "1",
"OPE": "AIRCEL",
"IPH": "Images/provider/aircelsm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 2,
"OCD": "3",
"OPE": "AIRTEL",
"IPH": "Images/provider/airtelsm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 4,
"OCD": "4",
"OPE": "BSNL",
"IPH": "Images/provider/bsnlsm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 6,
"OCD": "5",
"OPE": "DOCOMO",
"IPH": "Images/provider/docomosm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 7,
"OCD": "6",
"OPE": "IDEA",
"IPH": "Images/provider/ideasm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 8,
"OCD": "7",
"OPE": "MTS",
"IPH": "Images/provider/mtssm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 5,
"OCD": "8",
"OPE": "RELAINCE",
"IPH": "Images/provider/reliancesm.jpg",
"MIL": 10,
"MXL": 10
},
{
"OID": 3,
"OCD": "9",
"OPE": "VODAFONE",
"IPH": "Images/provider/vodafonesm.jpg",
"MIL": 10,
"MXL": 10
}
]
}
}
How to get the image from the server and save the image in arraylist . The arraylist data are viewed in the listview. I tried some methods but the issue is not solved .Please explain the step by step
The tried code for reading the image from server
JSONObject json = null;
try {
json = new JSONObject(result.getResult().toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = null;
jsonRootObject = json.optJSONObject("DS");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("LST");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
String iph = null;
String oid = jsonObject.optString("OID").toString();
String ocd = jsonObject.optString("OCD").toString();
String opd = jsonObject.optString("OPE").toString();
String mil = jsonObject.optString("MIL").toString();
String mxl = jsonObject.optString("MXL").toString();
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setOid(oid);
spinnerData.setOcd(ocd);
spinnerData.setOpd(opd);
spinnerData.setMil(mil);
spinnerData.setMix(mxl);
spinnerData.setImage(mIcon11);
selectedNetwork.add(spinnerData);
}
}
This is the code used for to save the image in arraylist
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
Upvotes: 0
Views: 56
Reputation: 11632
Instead of download and storing the image data by yourself, you can just save the image path and you can use Glide
lib to handle the image.
Just change your setImage(Bitmap) to setImage(String), and just store the path what you get in Json
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setImage(urldisplay);
And while loading image just give that to glide
For Glide:
Add this in your build.gradle
compile 'com.github.bumptech.glide:glide:3.5.2'
while displaying the image in UI you can do like this,
SpinnerMenu spinnerData = item.get(position)
ImageView imageview = // your image view;
Glide.with(context).load(spinnerData.getImage()).into(imageview);
Upvotes: 1