Reputation:
How can I open an image chooser and show an option to take a picture as well?
Here's my code:
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
The problem is that there's no open camera option. How do I add that?
And this is what I get:
Upvotes: 0
Views: 293
Reputation: 2217
You have to specify an action for invoking a chooser/app via an intent. And since the action for selecting an image from gallery and taking a new image using the camera, are different, i don't think a chooser will have both option for uploading an image and taking a new image at the same time. For reference you could check profile picture changing procedure in facebook or whatsaapp. When clicking on the image to change, a custom dialog is shown with the option to either upload an image, take a new image, or select from a previously uploaded image. Based on what the user selects the app navigates the user to the particular app(camera/gallery etc)
What you can do is first create a custom dialog(or a layout based on your application's design) with two options
Based on what the user clicks execute the code to navigate the user to, either the gallery or the camera application. If the user clicks on Upload an image from gallery, execute your above code and if the user clicks on Take a new image, then execute the code for opening the camera.
private void takeNewImage() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,CAMERA_INTENT_REQ_CODE);
}
Upvotes: 1
Reputation: 38
You can use this library or show a normal alert dialog with two buttons, one for gallery and one for camera.
There is a similar question: Dialog to pick image from gallery or from camera
Upvotes: 1