允许用户为图像选择相机或图库 [英] Allow user to select camera or gallery for image

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

问题描述

我想要做的事情看起来很简单,但经过几天的搜索,我无法完全弄清楚.

What I'm trying to do seems very simple, but after a few days of searching I can't quite figure it out.

我有一个应用程序,允许用户选择多个(最多 5 个)图像.我正在使用 ImageView.当用户点击 ImageView 时,我想允许他们选择

I have an application that allows the user to select multiple(up to 5) images. I'm using an ImageView. When the user clicks on the ImageView, I'd like to allow them the option to

  1. 从图库中选择图像,或
  2. 使用相机拍摄图像.

我开始使用 ACTION_GET_CONTENT 意图,这对于进入画廊非常有效.然后我尝试使用 ACTION_PICK_ACTIVITY 意图来允许用户选择相机或画廊:

I started by using the ACTION_GET_CONTENT intent, and that works well for getting to the gallery. So then I tried using the ACTION_PICK_ACTIVITY intent to allow the user to choose camera or gallery:

Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*"); 
Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent)
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, IMAGE_SELECTOR);

但似乎我只能添加一个 EXTRA_INTENT.菜单按预期显示,但唯一的选项是画廊和文件......没有相机).

But it appears I can only add one EXTRA_INTENT. The menu show up as expected, but the only options are Gallery and Files....no Camera).

有没有更好/更简单的方法来做到这一点,而我却错过了?谢谢你的帮助.

Is there a better/easier way to do this that I'm missing? Thanks for any help.

推荐答案

您必须创建自己的选择器对话框,合并两个意图解析结果.

You'll have to create your own chooser dialog merging both intent resolution results.

为此,您需要使用 PackageManager.queryIntentActivities() 用于两个原始意图,并为每个检索到的活动创建一个可能的意图的最终列表,如下所示:

To do this, you will need to query the PackageManager with PackageManager.queryIntentActivities() for both original intents and create the final list of possible Intents with one new Intent for each retrieved activity like this:

List<Intent> yourIntentsList = new ArrayList<Intent>();

List<ResolveInfo> listCam = packageManager.queryIntentActivities(camIntent, 0);
for (ResolveInfo res : listCam) {
    final Intent finalIntent = new Intent(camIntent);
    finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
    yourIntentsList.add(finalIntent);
}

List<ResolveInfo> listGall = packageManager.queryIntentActivities(gallIntent, 0);
for (ResolveInfo res : listGall) {
    final Intent finalIntent = new Intent(gallIntent);
    finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
    yourIntentsList.add(finalIntent);
}

(我直接在这里写的,所以这可能无法编译)

(I wrote this directly here so this may not compile)

然后,有关从列表创建自定义对话框的更多信息,请参阅 https://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

Then, for more info on creating a custom dialog from a list see https://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

这篇关于允许用户为图像选择相机或图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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