我想做的事情看起来很简单,但经过几天的搜索,我不太明白。
我有一个应用程序,允许用户选择多个(最多 5 个)图像。我正在使用 ImageView
。当用户单击 ImageView
时,我希望允许他们选择
- 从图库中选择图像或
- 使用相机捕获图像。
我首先使用 ACTION_GET_CONTENT
意图,这对于访问画廊非常有效。因此,我尝试使用 ACTION_PICK_ACTIVITY
意图来允许用户选择相机或图库:
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
。菜单按预期显示,但唯一的选项是图库和文件......没有相机)。
有没有更好/更简单的方法来做到这一点,我错过了?感谢您的任何帮助。
What I'm trying to do seems very simple, but after a few days of searching I can't quite figure it out.
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
- Select the image from the gallery, or
- Use the camera to capture an image.
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);
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.
发布评论
评论(16)
如何启动单个 Intent 以从图库或相机或任何注册用于浏览文件系统的应用程序中选择图像。
而不是创建包含 Intent 列表的对话框选项,最好使用 Intent.createChooser 来访问各种“相机”、“图库”甚至第三方文件系统浏览器应用程序(例如“Astro”等)的图形图标和短名称。
这描述了如何使用标准选择器意图并向其添加其他意图。
How to launch a single Intent to select images from either the Gallery or the Camera, or any application registered to browse the filesystem.
Rather than creating a Dialog with a list of Intent options, it is much better to use Intent.createChooser in order to get access to the graphical icons and short names of the various 'Camera', 'Gallery' and even Third Party filesystem browser apps such as 'Astro', etc.
This describes how to use the standard chooser-intent and add additional intents to that.
您必须创建自己的选择器对话框来合并两个意图解析结果。
为此,您需要使用 PackageManager.queryIntentActivities() 对于两个原始意图,并为每个检索到的活动创建一个可能的意图的最终列表,并使用一个新的意图,如下所示:(
我直接在这里写了这个,所以这可能不会编译)
然后,有关从列表创建自定义对话框的更多信息,请参阅 https://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
You'll have to create your own chooser dialog merging both intent resolution results.
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:
(I wrote this directly here so this may not compile)
Then, for more info on creating a custom dialog from a list see https://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
我发现了这个。使用:
作为意图之一,向用户显示在 Android 4 中选择“文档”的选项,我发现这非常令人困惑。使用它来代替显示“图库”选项:
I found this. Using:
for one of the intents shows the user the option of selecting 'documents' in Android 4, which I found very confusing. Using this instead shows the 'gallery' option:
我也遇到了这个问题,我所做的是创建一个 AlertDialog 并使用 setItems() 方法以及 DialogInterface 侦听器:
I also had this issue and what I did was create an AlertDialog and use the setItems() method along with the DialogInterface listener:
我合并了一些解决方案来制作一个完整的实用程序,用于从图库或相机中选取图像。这些是 ImagePicker util 的功能(也在 Github lib):
屏幕截图:
< img src="https://i.sstatic.net/CDzuX.png" alt="ImagePicker 起始意图">
编辑:这是获取合并 Intent 的代码片段一起使用图库和相机应用程序。
您可以在 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):
这应该可以解决 Tina 的 null outputFileUri 问题:
注意:我在 android.support.v4.app.Fragment 中使用此代码,您的重写方法可能会根据您使用的 Fragment/Activity 版本而改变。
This should take care of Tina's null outputFileUri issue:
Note: I'm using this code inside android.support.v4.app.Fragment your overridden methods might change depending on what Fragment/Activity version you're using.
您可以创建选项对话框
打开相机:
打开图库:
获取选择结果
保存捕获图像的方法
You can create option dialog
Open Camera:
Open Gallery:
To get selection result
Method to save captured image
对于那些在 4.4 以上版本尝试使用图像选择时遇到错误的人可以使用下面的代码。
与其创建包含 Intent 选项列表的对话框,不如使用 Intent.createChooser 来访问各种“相机”、“图库”甚至第三方文件系统浏览器应用程序的图形图标和短名称例如“Astro”等。
这描述了如何使用标准选择器意图并向其添加其他意图。
For those getting error on 4.4 upward while trying to use the Image selection can use the code below.
Rather than creating a Dialog with a list of Intent options, it is much better to use Intent.createChooser in order to get access to the graphical icons and short names of the various 'Camera', 'Gallery' and even Third Party filesystem browser apps such as 'Astro', etc.
This describes how to use the standard chooser-intent and add additional intents to that.
解决了图像太大的问题并避免内存不足。
Resolved too big image issue and avoid to out of memory.
添加我的解决方案 - 它将返回一个回调,无论是来自相机还是来自厨房以及意图:
Adding my solution - it will return a callback whether it is from the camera or from the galley alongside the intent:
你可以试试这个:
You can try this :
通过使用 AlertDialog 和 Intent.ACTION_PICK 这很简单
This is simple by using AlertDialog and Intent.ACTION_PICK
根据大卫的回答,我在
onActivityResult()
部分花费了两分钱。它负责 5.1.1 中引入的更改,并检测用户是否从库中选择了单个或多个图像。Building upon David's answer, my two pennies on the
onActivityResult()
part. It takes care of the changes introduced in 5.1.1 and detects whether the user has picked a single or multiple image from the library.尝试这种方式
然后创建 onactivityresult 方法并执行类似的操作
请参阅此 http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample
Try this way
Then create onactivityresult method and do something like this
See this http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample
我在相机或图库图像选择方面付出了很多努力,并为此工作创建了一些 util 类。
使用这些类“选择图像相机或图库太容易了”
只需 5 到 10 分钟即可完成开发。
ImagePickerUtils :- http://www.codesend.com/view/f8f7c637716bf1c693d1490635ed49b3/
BitmapUtils :-
http://www.codesend.com/view/81c1c2a3f39f1f7e627f01f67be282cf/
ConvertUriToFilePath : -
http://www.codesend.com/view/f4668a29860235dd1b66eb419c5a58b5/
MediaUtils: - https://codeshare.io/5vKEMl
我们需要添加这些权限menifest:
这个类函数 (checkAndRequestPermissions) 自动检查 Android-Marshmallow 和 Android - Nougat 中的权限。
我希望它对您有所帮助,如果有人有任何改进这些课程的建议,请在评论中添加您的评论。 >
I had worked hard on Camera or gallery Image Selection, and created some util class for this work.
With the use of these class 'Selecting image camera or gallery is too easy'
it just took 5-10 mints of your development.
ImagePickerUtils :- http://www.codesend.com/view/f8f7c637716bf1c693d1490635ed49b3/
BitmapUtils :-
http://www.codesend.com/view/81c1c2a3f39f1f7e627f01f67be282cf/
ConvertUriToFilePath :-
http://www.codesend.com/view/f4668a29860235dd1b66eb419c5a58b5/
MediaUtils :- https://codeshare.io/5vKEMl
We need to Add these Permission in menifest :
This class function (checkAndRequestPermissions) auto check the permission in Android-Marshmallow and Android - Nougat.
I hope it helps you, If any one having any suggestion to improve these class please add your review in comments.
根据 David Manpearl 的回答
https://stackoverflow.com/a/12347567/7226732
我们只需要修改 onActivityResult( ) 就像
并在图像视图中设置捕获或挑选图像。
As per David Manpearl answer
https://stackoverflow.com/a/12347567/7226732
we just need to modify onActivityResult() like
and set the capture or pick images in image view.