Android – Choose image from gallery or take from camera

androidcameragallery

I have to make a button that will provide to choose image from gallery or take from camera.

 private void showFileChooser() {
    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePicture, 0);
    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(pickPhoto , 1);

}

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

The result is working. If i choose from gallery, the imageviewer will view that, it also working if i choose taking photo from camera.
The problem is, in my showFileChooser() method, all of my intent are runing in same time, so when i choos from gallery, the camera still running also. I choose camera, the gallery is opening too. I think i should implement my code in switch case mode but i dont understand how to do that. Please kindly help to solve my beginner problem.

Best Answer

Add a alertDialog and then in onActivityResult instead of case of 0 and 1 use the REQUEST_CAMERA and SELECT_FILE. which you'll declare as in code:

private static final int REQUEST_CAMERA = 1;
private static final int SELECT_FILE = 2;

final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mActivity);
            builder.setTitle("Add Photo!");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {

                    if (items[item].equals("Take Photo")) {
                        PROFILE_PIC_COUNT = 1;
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, REQUEST_CAMERA);
                    } else if (items[item].equals("Choose from Library")) {
                        PROFILE_PIC_COUNT = 1;
                        Intent intent = new Intent(
                                Intent.ACTION_PICK,
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent,SELECT_FILE);
                    } else if (items[item].equals("Cancel")) {
                        PROFILE_PIC_COUNT = 0;
                        dialog.dismiss();
                    }
                }
            }); 
            builder.show();
Related Topic