0

我有一个在 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

4 回答 4

3

一旦您使用json 解析将图像路径解析为 url ..

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();
}
于 2012-10-03T03:01:55.720 回答
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后得到的图片的路径。

于 2012-10-03T02:59:12.343 回答
2

解析您的 json 并获取 URL,然后将它们放在我们的 URL 中 方法

于 2012-10-03T02:59:47.460 回答
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();
        }
    }
于 2012-10-03T05:18:09.123 回答