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

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

问题描述

我想要做的似乎很简单,但经过搜索几天我不能完全弄清楚。

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与<一个href="http://d.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29">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)

然后,从列表中创建自定义对话框的详细信息请参见<一href="http://developer.android.com/intl/fr/guide/topics/ui/dialogs.html#AlertDialog">http://developer.android.com/intl/fr/guide/topics/ui/dialogs.html#AlertDialog

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

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

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