2013-10-28 45 views
0

如何將我作爲JSON獲取的圖像設置爲imageview?下面是我的代碼..帶有JSON數據的setImageResource

try { 
       JSONObject responseObject = json.getJSONObject("responseData"); 
       JSONArray resultArray = responseObject.getJSONArray("results"); 
       private ArrayList<Object> listImages; 
       listImages = getImageList(resultArray); 
       } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 

現在,我想設置圖像下面的ImageView ...

private int[] GalImages = new int[] { R.drawable.h, R.drawable.i}; 
    public Object instantiateItem(ViewGroup container, int position) { 
      ImageView imageView = new ImageView(context); 
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); 
      imageView.setPadding(padding, padding, padding, padding); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
      imageView.setImageResource(GalImages[position]); 

      ((ViewPager) container).addView(imageView, 0); 
      return imageView; 
     } 

在這裏,我通過繪製文件夾的默認圖像設置,但我設置我作爲JSON獲取的圖像,我該如何做到這一點?

回答

0

在這裏,我通過繪製文件夾的默認圖像設置,但我已經設置 圖像,我得到的JSON,我該怎麼辦呢?

用於顯示來自web url的圖像,您將從服務器返回的JSON中獲取您需要先下載可用緩存或存儲中的圖像。下載圖像後得到的位圖,並把它傳遞給ImageView.setImageBitmap

private Bitmap download_Image(String url) { 

     Bitmap bmp =null; 
     try{ 
      URL ulrn = new URL(url); 
      HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
      InputStream is = con.getInputStream(); 
      bmp = BitmapFactory.decodeStream(is); 
      if (null != bmp) 
       return bmp; 

      }catch(Exception e){} 
     return bmp; 
    } 

呼叫使用AsyncTaskHandler避免NetworkOnMainThreadExceptiondownload_Image方法。

如果你正在使用的ListView或GridView中查看,然後使用Universal Image Loader

+0

我不能做到這一點,而無需下載? –

+0

@thumbernirmal:是不可能通過url將src設置爲ImageView –