让用户拍照或从 Android 中的图库中选择图像的单一意图 [英] Single intent to let user take picture OR pick image from gallery in Android

查看:30
本文介绍了让用户拍照或从 Android 中的图库中选择图像的单一意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 2.1 及更高版本开发应用程序.我想让我的用户在我的应用程序中选择个人资料图片(我没有使用联系人框架).

I'm developing an app for Android 2.1 upwards. I want to enable my users to select a profile picture within my app (I'm not using the contacts framework).

理想的解决方案是触发一个意图,使用户能够从图库中选择图像,但如果没有合适的图像可用,则使用相机拍照(反之亦然,即允许用户拍摄图片,但如果他们知道他们已经有合适的图像,让他们进入画廊并选择所述图像).

The ideal solution would be to fire an intent that enables the user to select an image from the gallery, but if an appropriate image is not available then use the camera to take a picture (or vice-versa i.e. allow user to take picture but if they know they already have a suitable image already, let them drop into the gallery and pick said image).

目前我可以做一个或另一个,但不能同时做.

Currently I can do one or the other but not both.

如果我使用 MediaStore.ACTION_IMAGE_CAPTURE 直接进入相机模式,则没有选项可以放入图库.

If I go directly into camera mode using MediaStore.ACTION_IMAGE_CAPTURE then there is no option to drop into the gallery.

如果我使用 Intent.ACTION_PICK 直接进入画廊,那么我可以选择一张图像,但如果我点击相机按钮(在画廊的右上角),则会触发一个新的相机意图.因此,拍摄的任何照片都不会直接返回到我的应用程序.(当然你可以按返回按钮回到画廊并从那里选择图像,但这是一个额外的不必要的步骤,而且一点也不直观).

If I go directly to the gallery using Intent.ACTION_PICK then I can pick an image but if I click the camera button (in top right hand corner of gallery) then a new camera intent is fired. So, any picture that is taken is not returned directly to my application. (Sure you can press the back button to drop back into the gallery and select image from there but this is an extra unnecessary step and is not at all intuitive).

那么有没有办法将两者结合起来,还是我必须提供一个菜单来在我的应用程序中执行其中一个或另一个?似乎这将是一个常见的用例......我肯定错过了什么吗?

So is there a way to combine both or am I going to have to offer a menu to do one or the other from within my application? Seems like it would be a common use case...surely I'm missing something?

推荐答案

UPDATE:在这一点上,使用 EXTRA_INITIAL_INTENTS 的另一个答案更好.在我写答案的时候,EXTRA_INITIAL_INTENTS 还不存在,因为它是在 API 级别 5 中添加的.

UPDATE: The other answer, using EXTRA_INITIAL_INTENTS, is a better one at this point. At the time I wrote my answer, EXTRA_INITIAL_INTENTS did not yet exist, as it was added in API Level 5.

那么有没有办法将两者结合起来或我将不得不提供一个菜单从我的内部做一个或另一个申请?

So is there a way to combine both or am I going to have to offer a menu to do one or the other from within my application?

编写您自己的画廊,拥有您想要的功能.

Write your own gallery that has the features you desire.

我认为菜单会更简单.

这似乎是一个常见的用途案例...我肯定遗漏了什么?

Seems like it would be a common use case...surely I'm missing something?

您旁边的开发人员会认为图库应该允许您从本地图库中进行选择,或者跳到 Flickr 从那里进行选择.另一位开发人员会认为相机不仅应该允许通过相机拍照",还应该通过从图库中选择一些东西来拍照",从而将事物与你想象的方式颠倒过来.另一个开发人员会认为画廊应该允许从本地画廊、Flickr、相机或网络连接的网络摄像头中进行选择.还有一个开发人员会认为图库很愚蠢,用户应该只通过文件浏览器选择文件.以此类推.

The developer next to you will think the gallery should allow you to pick from the local gallery or else hop out to Flickr to make a selection from there. Another developer will think the camera should not only allow to "take a picture" via the camera but to "take a picture" via choosing something from the gallery, inverting things from the way you envision it. Yet another developer will think that the gallery should allow picking from the local gallery, or Flickr, or the camera, or a network-attached webcam. Still another developer will think that the gallery is stupid and users should just pick files via a file explorer. And so on.

所有这些都在操作系统闪存非常重要的环境(手机)中进行.

All of this in an environment (mobile phones) where flash for the OS is at a premium.

因此,恕我直言,核心 Android 团队选择提供构建块供您按照您认为合适的方式组装,而不是试图适应所有可能的模式,这并不完全令人震惊.

Hence, IMHO, it is not completely shocking that the core Android team elected to provide building blocks for you to assemble as you see fit, rather than trying to accommodate every possible pattern.

这篇关于让用户拍照或从 Android 中的图库中选择图像的单一意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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