2012-10-03 50 views
0

我有一個在json中提供圖像路徑的網站。我想要得到這個路徑並在我的ImageView中顯示圖像。從Web獲取圖像的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 

回答

2

解析您的JSON和採取的URL,然後把它們放在我們的網址在the methodhere

+0

你能做到嗎?請參閱:http://stackoverflow.com/questions/11504830/jsonexception-when-parse-a-valid-json更多信息 –

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。

+0

我有嘗試但這是行不通的。你能給我一個簡單的項目嗎?在此先感謝[電子郵件]([email protected]) – kongkea

+0

我認爲你得到的解決方案.. –

+0

是的,不過,謝謝你 – kongkea

3

一旦你解析圖像路徑使用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(); 
} 
+0

我試過但它不起作用。你能給我一個簡單的項目嗎?在此先感謝([email protected]) – kongkea

+0

是否通過解析 –

+0

得到一個網址,即使您的圖像不顯示在瀏覽器中。檢查它 –

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(); 
     } 
    }