2012-03-07 64 views
0

我在我的xml文件中使用圖像視圖和圖庫。我正在爲webservice(遠程圖像)獲取圖庫圖像。在android中顯示圖庫遠程圖像imageView

當我從圖庫中選擇圖像我想要顯示在Android圖像視圖。

這裏我的代碼,

ImageAdapter.java 

public class ImageAdapter extends BaseAdapter 
{ 
    private Context context = null; 
    DataService Ds; 
    LayoutInflater mInflater; 

    private String[] myRemoteImages; 



    public ImageAdapter(Context context) 
     { 
      this.context = context; 
      //myRemoteImages = imgArray; 
      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     } 

    @Override 
    public int getCount() 
     { 
      return this.myRemoteImages.length; 
     } 

    @Override 
    public final Object getItem(int position) 
     { 
      return position; 
     } 

    @Override 
    public long getItemId(int position) 
     { 
      return position; 
     } 

    @Override 
    public View getView(int position, View view, ViewGroup group) 
     { 
      ImageView imageView = new ImageView(this.context);   

      try { 

       URL aURL = new URL(myRemoteImages[position]); 
       System.err.println(aURL); 
       URLConnection conn = aURL.openConnection(); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       BufferedInputStream bis = new BufferedInputStream(is); 
       Bitmap bm = BitmapFactory.decodeStream(bis); 
       bis.close(); 
       is.close(); 

       imageView.setImageBitmap(bm); 

      } catch (IOException e) { 
       //imageView.setImageResource(R.drawable.nopic); 
       Log.e("DEBUGTAG", "Remote Image Exception", e); 
      } 
      imageView.setImageResource(position); 
      imageView.setLayoutParams(new Gallery.LayoutParams(105, 70)); 
      return imageView; 
     } 

    public String[] getMyRemoteImages() { 
     return myRemoteImages; 
    } 

    public void setMyRemoteImages(String[] myRemoteImages) { 
     this.myRemoteImages = myRemoteImages; 
    } 
} 


and DisplayImage.java 

ImageView imgView; 

gallery = (Gallery) this.findViewById(R.id.Gallery01); 
     imageAdapter = new ImageAdapter(this); 
     imageAdapter.setMyRemoteImages(ItemList.getImages()); 
     gallery.setAdapter(imageAdapter); 

     gallery.setOnItemClickListener(new OnItemClickListener() 
     { 
      public void onItemClick(AdapterView parent, 
      View v, int position, long id) 
      {   
       System.out.println("gallery position" +imageAdapter.getItemViewType(position)); 
       imgView.setImageResource(imageAdapter.getItemViewType(position)); 


      } 
     }); 
     } 

任何一個能幫助我嗎?

在此先感謝

回答

0

現在它的工作。這裏是我的代碼,

DisplayImage.java:

ImageView imgView; 

gallery.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, int position, 
        long id) { 
       System.out.println("Gallery position :" 
         + bikeItemList.getOriginalImages().get(position)); 
       UrlImageViewHelper.setUrlDrawable(imgView, bikeItemList 
         .getOriginalImages().get(position)); 
      } 
     });