Android Choose Photo from Camera and Gallery

Friends  today we are going to learn about choose photo from camera and gallery,we can choose photo easily with the help of Intent .For getting image from camera and gallery follow below step by step.

1, Choose image from camera.
for choose image from camera we have to set action(MediaStore.ACTION_IMAGE_CAPTURE) on Intent  and set request code on startActivityForResult.

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(takePicture, 0)//zero is a request code

 2, Choose image from gallery.
for choose image from camera we have to set action(Intent.ACTION_PICK,
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) on Intent  and set request code on startActivityForResult.

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one is a request code 


3. GetFinalImage.
after choosing photo from camera and gallery we work onActivityResult method getting image,follow blow code for getting image.

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
switch(requestCode) {
case 0:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        imageview.setImageURI(selectedImage);
    }
 
break; 
case 1:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        imageview.setImageURI(selectedImage);
    }
break;
}
}