`

从网络获取图片

 
阅读更多
@Override
    public View getView(int position, View convertView,
        ViewGroup parent)
    {
      // TODO Auto-generated method stub
      /* 建立一个ImageView对象 */

      ImageView imageView = new ImageView(this.myContext);
      try
      {
        /* new URL物件将网址传入 */
        URL aryURI = new URL(myImageURL[position]);
        /* 取得联机 */
        URLConnection conn = aryURI.openConnection();
        conn.connect();
        /* 取得回传的InputStream */
        InputStream is = conn.getInputStream();
        /* 将InputStream变成Bitmap */
        Bitmap bm = BitmapFactory.decodeStream(is);
        /* 关闭InputStream */
        is.close();
        /* 设定Bitmap于ImageView中 */
        imageView.setImageBitmap(bm);
      } catch (IOException e)
      {
        e.printStackTrace();
      }

      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      /* 设定这个ImageView对象的宽高,单位为dip */
      imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));
      /* 设定Gallery背景图 */
      imageView.setBackgroundResource(mGalleryItemBackground);
      return imageView;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics