0

I've got a website that provide a path of image in json. and I want to get this path and display an image in my ImageView.

Hint: targetSdkVersion="15"

Example:

{
"count": "28",
"data": [
    {
        "id": "84",
        "thumb": "http://mmmmm.cccc.com/data/card/thum/a1f694f5ba0df9147c02b0c0c8cb83c2.jpg",
        "category": "Purchase",
        "title": "test",
        "locationname": "test",
        "latitude": "0",
        "longitude": "0"
    }
]
}

In my Activity:

ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.photo); // this show image that has in project and how about display image that using JSON path above
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
kongkea
  • 9,788
  • 12
  • 39
  • 49

4 Answers4

3

once the you parse the image path to url using json parsing..

url = "http://mmmmm.cccc.com/data/card/thum/a1f694f5ba0df9147c02b0c0c8cb83c2.jpg";

try {
  ImageView i = (ImageView)findViewById(R.id.imageView1);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
  i.setImageBitmap(bitmap); 
  } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
2

Try this

try{

URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
    iv.setImageBitmap(bmp);
else
    System.out.println("The Bitmap is NULL");

}catch(Exception e){}
}

Here url is the path of the image that you got after jsonparsing.

G_S
  • 7,068
  • 2
  • 21
  • 51
  • I have try but It's doesn't work. can u give me one simple project. thanks in advance [email](allmyemail.inonemail@gmail.com) – kongkea Oct 03 '12 at 05:06
  • can you help me one more time, look at this link [org.json.JSONObject cannot be converted to JSONArray in android](http://stackoverflow.com/questions/12722468/org-json-jsonobject-cannot-be-converted-to-jsonarray-in-android) – kongkea Oct 04 '12 at 07:45
2

Parse your json and take the URL and then put them us URL in the method or here

Community
  • 1
  • 1
Nam Vu
  • 5,669
  • 7
  • 58
  • 90
  • Can you do it? See: http://stackoverflow.com/questions/11504830/jsonexception-when-parse-a-valid-json for more info – Nam Vu Oct 03 '12 at 06:41
1
String[] imageArray=null;
JSONObject json;
try {
      JSONObject jObject = new JSONObject(Your result Object here);
      json = jObject;
      JSONArray jdataObject = json.getJSONArray("data");
      jobOrderCodeArray = new String[jdataObject.length()];

      for(int i=0;i<jdataObject.length();i++){
          JSONObject jobj = jdataObject.getJSONObject(i);
          imageArray[i] = jobj.getString("thumb");
        }
    }
    catch(Exception e){
       e.printStackTrace();
    }


  for (int i = 0; i < imageArray.length; i++) {
        try {
          ImageView iv = (ImageView) findViewById(R.id.imageView1);
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageArray[i]).getContent());
          iv.setImageBitmap(bitmap); 
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
Harish
  • 3,122
  • 2
  • 31
  • 46