Choosing between camera and gallery for image selection
O

2

8

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?

Outlay answered 2/6, 2011 at 18:12 Comment(0)
T
7

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 and btnTakePicture.

Both buttons fire their own action, say onBtnPickGallery and onBtnTakePicture.

public void onBtnPickGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST_CODE);
}

public void onBtnTakePicture() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(), "dir/pic.jpg");

    Uri outputFileUri = Uri.fromFile(photo);

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}

And then you can grab the result using onActivityResult() method.

Tevet answered 2/6, 2011 at 19:16 Comment(0)
C
14

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):

  • Merged intents for Gallery and Camera resquests.
  • Resize selected big images (e.g.: 2500 x 1600)
  • Rotate image if necesary

Screenshot:

ImagePicker starting intent

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)

public static Intent getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra("return-data", true);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
    intentList = addIntentsToList(context, intentList, pickIntent);
    intentList = addIntentsToList(context, intentList, takePhotoIntent);

    if (intentList.size() > 0) {
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getString(R.string.pick_image_intent_text));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
    }

    return chooserIntent;
}

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        Intent targetedIntent = new Intent(intent);
        targetedIntent.setPackage(packageName);
        list.add(targetedIntent);
    }
    return list;
}
Crowell answered 9/9, 2015 at 9:26 Comment(5)
Thank you, it's exactly what I was looking for (and the OP too, I bet). I just polished the code a little (for instance removing the internal class wrapping rotateOrientationCall and small things like that), but it's working really well ^^Gaytan
Thanks @llario feel free to fork or comment the gist to improve the code.Crowell
I have improved the rotation for Gallery images, and the resize method. Check it out.Crowell
While the given link may answer the question, it is better to include the essential parts of your code here and provide the link for reference. Link-only answers can become invalid if the linked page changes. We would like to preserve self-containing answers here on SO.Palestine
Thank you @MarioVelasco this helps a lot, though i tweak some parameter as per my need.Telegu
T
7

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 and btnTakePicture.

Both buttons fire their own action, say onBtnPickGallery and onBtnTakePicture.

public void onBtnPickGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST_CODE);
}

public void onBtnTakePicture() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(), "dir/pic.jpg");

    Uri outputFileUri = Uri.fromFile(photo);

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}

And then you can grab the result using onActivityResult() method.

Tevet answered 2/6, 2011 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.