【问题标题】:Get JSON path of image from web to display image in ImageView从 Web 获取图像的 JSON 路径以在 ImageView 中显示图像
【发布时间】:2012-10-03 02:41:04
【问题描述】:

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

【问题讨论】:

    标签: android json imageview


    【解决方案1】:

    一旦您使用 json parsing.. 将图像路径解析为 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();
    }
    

    【讨论】:

    • 我试过了,但它不起作用。你能给我一个简单的项目吗?在此先感谢(allmyemail.inonemail@gmail.com)
    • 你是不是通过解析得到一个url
    • 即使您的图片没有显示在浏览器中。检查一下
    【解决方案2】:

    解析您的 json 并获取 URL,然后将它们放入我们的 URL 中 the methodhere

    【讨论】:

    【解决方案3】:

    试试这个

    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是json解析后得到的图片的路径。

    【讨论】:

    【解决方案4】:
    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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2011-11-16
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多