2014-04-08 2 views
-1

안드로이드에있는 버튼을 클릭하면 갤러리에서 이미지를 얻는 방법은 무엇입니까? 저는 안드로이드를 처음 접하는데 도와주세요.갤러리에서 이미지를 가져 와서 이미지 뷰에 표시하는 방법은 무엇입니까?

+0

어쩌면이 질문에 도움이 당신에게 http://stackoverflow.com/questions/2507898/how-to-pick-an- 갤러리에서 만든 이미지 - 카드 - 내 - 애플 리케이션 - 안드로이드 –

+2

거기에 사용할 수있는 게시물/튜토리얼이 너무 많습니다. 여기에 질문을 올리기 전에 꼭 봤습니까? –

답변

0

이 코드를 확인하는 것은 매우 쉽습니다. 행운

public class MainActivity extends Activity { 

Button btn1; 
ImageView img; 
private static int LOAD_IMAGE_RESULTS = 1; 
Bitmap mBitmap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn1 = (Button) findViewById(R.id.button1); 
    edit = (EditText) findViewById(R.id.editText1); 
    img = (ImageView) findViewById(R.id.ImageView1); 

    img.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

      // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery. 
      startActivityForResult(i, LOAD_IMAGE_RESULTS); 

     } 
    }); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Here we need to check if the activity that was triggers was the Image Gallery. 
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value. 
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked. 
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) { 
     // Let's read picked image data - its URI 
     Uri pickedImage = data.getData(); 
     // Let's read picked image path using content resolver 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); 
     cursor.moveToFirst(); 
     imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

     // Now we need to set the GUI ImageView data with data read from the picked file. 
     mBitmap = BitmapFactory.decodeFile(imagePath); 
     img.setImageBitmap(BitmapFactory.decodeFile(imagePath)); 

     // At the end remember to close the cursor or you will end with the RuntimeException! 
     cursor.close(); 
    } 
} 

}

+0

몇 가지 아주 사소한 변화가 있습니다. 여기에서 표절했습니다 : https://gist.github.com/moltak/8160257 –

0
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i, RESULT_LOAD_IMAGE); 

그리고 u는

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      ImageView imageView = (ImageView) findViewById(R.id.imgView); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
    } 

을하여 onActivityResult에서 u는 사진 경로를 얻을 것이다 당신이 다음 코드를 사용하여 전체 코드를 HERE

0

를 얻기 위해 이것을 사용 완벽하게 작동합니다.

,210
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    Button yourbutton = (Button) (findViewById(R.id.button); 

    yourbutton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, TAKE_PICTURE); 

     } 
    }); 
} 

외부 행해져 Yout은 한 OnCreate :

행운 :)의
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 


    switch(requestCode){ 

     case TAKE_PICTURE:    
      if(resultCode==Activity.RESULT_OK) { 

       // get bundle 
       Bundle extras = data.getExtras(); 

       // get 
       takenPictureData = (Bitmap) extras.get("data"); 
      // imageView.setImageBitmap(bitMap); 
      }    
      break; 
    } 

    //And show the result in the image view when take picture from camera. 
    if(takenPictureData!=null){ 
     imageView.setImageBitmap(takenPictureData); 
    }  
} 

최저

관련 문제