在相机和图库之间进行选择以进行图像选择 [英] Choosing between camera and gallery for image selection

查看:41
本文介绍了在相机和图库之间进行选择以进行图像选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图允许用户从图库中或通过使用相机拍照来选择图像.我试过这个:

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.

我建议您使用按钮(或任何让用户选择操作的 UI)并为这两个操作创建两个单独的方法.假设您已经创建了两个名为 btnPickGallerybtnTakePicture 的按钮.

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.

两个按钮都会触发自己的动作,比如 onBtnPickGalleryonBtnTakePicture.

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() 方法获取结果.

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

这篇关于在相机和图库之间进行选择以进行图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆