2011-02-10 79 views
0

我想從gallery.For這個圖片我用如何獲取子項從圖庫android的項目?

View v= mGallery.childAt(index); 
    ImageView img = (ImageView) v.findViewById(R.id.img); 

但有時如果索引圖像是不可見那個時候它給

NullPointerException異常。

如何從圖庫中獲取不可見的圖像?

回答

1

我認爲你是通過一個適配器添加圖像畫廊? 如果是這樣,您可以使用getItemAtPosition(index)來基本檢索已放入適配器的內容。當然,你需要確保索引是有效的。

如果你想選擇的項目,你還有其他的選擇,讓你直接訪問: Gallery.getSelectedView()Gallery.getSelectedItem()

+0

Iam試圖獲得物品,但當畫廊移動位置正在改變?如何知道以前按下的圖像位置。我已經採取了變量的按下position.But該位置可能不會持續。 – ADIT 2011-02-10 11:09:27

+0

mGallery.getItemAtPostion(position);給對象。如果我把這個對象轉換成View v =(View)Object;它給出了castexception。請給我一個這個片段。 – ADIT 2011-02-10 12:18:59

0

使用類似這樣的東西。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
photoPickerIntent.setType("image/*"); 
startActivityForResult(photoPickerIntent, 1); 

onActivityResult方法:

protected void onActivityResult(int requestCode, int resultCode, 
     Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 

    if (resultCode == RESULT_OK) { 
     Uri photoUri = intent.getData(); 

     if (photoUri != null) { 
      try { 
       Bitmap bitmap = MediaStore.Images.Media.getBitmap(this 
        .getContentResolver(), photoUri); 
       your_imgv.setImageBitmap(bitmap); 
       profilePicPath = photoUri.toString(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

感謝..

+0

請給我任何解決上述問題如何從畫廊獲得兒童項目?我只需要上述only.Now無論你提到的方式是不是選擇我的情況。 – ADIT 2011-02-10 10:11:38

0

哦,這是一種方式和更好的方式好友2獲得的圖像,並顯示他們......(我描述在我上面的回答)....

2解決您的問題,我只能說明ü一些技巧或補丁2適用於我們的代碼...

的,你可以檢查空狀態。如果你得到空圖像,然後顯示一些默認圖像,以代替它........