Sergey Metlov Sergey Metlov - 2 months ago 36
Android Question

Dialog to pick image from gallery or from camera

Is there a standard way to call dialog box with choose either to pick an image from the camera or to get from gallery (like in build-in phone book or Skype)?

I've taken a look at this but the code opens gallery without suggesting to pick it from camera.

Device: Samsung Galaxy Tab

Android: 2.3.3

Answer

Below code can be used for taking and picking photo and for selection of either pick photo or take photo. Just show a dialog with two option and upon selection use the appropriate code.

To take picture from camera:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code

To pick photo from gallery:

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code

onActivityResult code:

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;
}
}

Finally add this permission in the manifest file:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />