摄像头和画廊之间选择的图像选择画廊、摄像头、图像

2023-09-13 01:16:20 作者:疯子丶

我想允许用户选择图像,无论是从画廊或拍照与摄像头。我想这样的:

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

但它会自动显示,甚至没有提供一个选项,选择一个活动的画廊。好像应该有一些更好的方法来做到这一点比this问题。那是真正的唯一办法做到这一点?

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.

我建议你用按钮(或任何UI更是令用户选择的动作),并创造了两项诉讼两个分开的方法。比方说,你已经创建了一个名为两个按钮 btnPickGallery btnTakePicture

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.

两个按钮触发自己的实际行动,说 onBtnPickGallery onBtnTakePicture

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

然后你可以抓住使用 onActivityResult()方法的结果。