I am trying to allow a user to select an image, either from the gallery or by taking a picture with the camera. I tried this:
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(Intent.createChooser(imageIntent, "Select Picture"), GET_IMAGE_REQUEST);
but it automatically displays the gallery without even providing an option to choose an activity. It seems like there should be some better way to accomplish this than the solution given in this question. Is that really the only way do it?
You should do this logic within your app. Picking image from gallery and taking picture using camera are using different intent.
I suggest you use button (or whatever UI it is to make a user select an action) and creates two separate method for both actions. Let's say, you've created two buttons named
btnPickGallery
andbtnTakePicture
.Both buttons fire their own action, say
onBtnPickGallery
andonBtnTakePicture
.And then you can grab the result using
onActivityResult()
method.I have merged some solutions to make a complete util for picking an image from Gallery or Camera. These are the features of ImagePicker util (also in a Github lib):
Screenshot:
Edit: Here is a fragment of code to get a merged Intent for Gallery and Camera apps together. You can see the full code at ImagePicker util (also in a Github lib)