2010-08-20 3 views

답변

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

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
switch (requestCode) { 
case 1: 
{ 
    if (resultCode == RESULT_OK) 
    { 
    Uri photoUri = data.getData(); 
    if (photoUri != null) 
    { 
    try { 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); 
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
String filePath = cursor.getString(columnIndex); 
cursor.close(); 
Bitmap bMap = BitmapFactory.decodeFile(filePath); 
image.setImageBitmap(bMap); 

}catch(Exception e) 
    {} 
    } 
} 
} 
} 

ImageView.의 이미지를 당신의 SD 카드에서 이미지를 선택하고 가져 오기 이미지 onActivityResult()를 사용하고 표시하는 데 사용이 의도 지금 우리는 갤러리에서 chossed 이미지를 얻을 후 이미지 뷰에 이미지를 설정 . image.setImageBitmap(bMap); 이미지를 ImageView로 설정하십시오.

+2

커서가 실제로 실행될 수 있는지 확인해야합니다. 그렇지 않은 경우 미디어가 반환되지 않으면 예외가 발생합니다. if (cursor.moveToFirst()) {main code} – mishkin

관련 문제