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

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have try but It's doesn't work. can u give me one simple project. thanks in advance ([email protected])
are you getting a url by parsing
even your image is not displaying in browser. check it
2

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

1 Comment

Can you do it? See: stackoverflow.com/questions/11504830/… for more info
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.

2 Comments

I have try but It's doesn't work. can u give me one simple project. thanks in advance [email]([email protected])
can you help me one more time, look at this link org.json.JSONObject cannot be converted to JSONArray in android
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();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.