在相机和图库之间选择图像选择
我试图允许用户从图库中或通过使用相机拍照来选择图像。我尝试了这个:
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(Intent.createChooser(imageIntent, "Select Picture"), GET_IMAGE_REQUEST);
但它会自动显示图库,甚至没有提供选择活动的选项。似乎应该有一些比 这个问题。这真的是唯一的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我合并了一些解决方案来制作一个完整的实用程序,用于从图库或相机中选取图像。这些是 ImagePicker util 的功能(也在 Github lib):
屏幕截图:
< img src="https://i.sstatic.net/CDzuX.png" alt="ImagePicker 起始意图">
编辑:这是用于合并的代码片段旨在将图库和相机应用程序结合在一起。
您可以在 ImagePicker util 中查看完整代码(也在 Github 库)
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):
Screenshot:
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)
您应该在您的应用程序中执行此逻辑。从图库中选取图像和使用相机拍照使用不同的意图。
我建议您使用按钮(或任何让用户选择操作的 UI)并为这两个操作创建两个单独的方法。假设您创建了两个名为
btnPickGallery
和btnTakePicture
的按钮。这两个按钮都会触发自己的操作,例如
onBtnPickGallery
和onBtnTakePicture
。然后您可以使用
onActivityResult()
方法获取结果。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
andbtnTakePicture
.Both buttons fire their own action, say
onBtnPickGallery
andonBtnTakePicture
.And then you can grab the result using
onActivityResult()
method.