从Web获取图像的JSON路径,以在ImageView中显示图像

时间:2012-10-03 02:41:05

标签: android json imageview

我有一个网站,在json中提供图像路径。我希望得到这条路径并在我的ImageView中显示图像。

提示:targetSdkVersion =" 15"

示例:

{
"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"
    }
]
}

在我的活动中:

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 个答案:

答案 0 :(得分:3)

一旦您使用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();
}

答案 1 :(得分:2)

试试这个

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){}
}

这里的url是你在jsonparsing之后得到的图像的路径。

答案 2 :(得分:2)

解析你的json并获取网址,然后在the methodhere

中将这些网址添加到我们这里

答案 3 :(得分: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();
        }
    }