2013-11-15 102 views

回答

2

在你的項目中添加這個類:JsonParser

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     Bitmap bitmap = null; 
     try { 

      URL aURL = new URL(params[0]); 
      URLConnection conn = aURL.openConnection(); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 

      // Buffered is always good for a performance plus. 
      BufferedInputStream bis = new BufferedInputStream(is); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize =5; 
      // Decode url-data to a bitmap. 
      bitmap = BitmapFactory.decodeStream(bis, null, options); 

      // Close BufferedInputStream 
      bis.close(); 

      // Close InputStream 
      is.close(); 

      return bitmap; 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
      return null; 
     } 

    } 

在主要活動: - 添加網格視圖和設置適配器 呼叫asyntask與您的網址

ImageDownloader d=new ImageDownloder(); 
Bimap b=d.execute("url").get(); 
grid = (GridView) findViewById(R.id.grid);// grid view ti view all photos 
AdapterForPics adap = new AdapterForPics(Arraylist, Context); 
grid.setAdapter(adap); 
3

你究竟想做什麼,因爲json解析和顯示圖像在gridview是兩個不同的東西。要麼從服務器捕捉圖像並使用json解析來解析網址。要麼讓您的問題清晰明瞭,以供所有人理解。