摄像头和画廊之间选择的图像选择 [英] Choosing between camera and gallery for image selection
问题描述
我想允许用户选择图像,无论是从画廊或拍照与摄像头。我想这样的:
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);
但它会自动显示,甚至没有提供一个选项,选择一个活动的画廊。好像应该有一些更好的方法来做到这一点比<给定的解决方案href="http://stackoverflow.com/questions/4455558/allow-user-to-select-camera-or-gallery-for-image">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()
方法的结果。
这篇关于摄像头和画廊之间选择的图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!