以编程方式从 Android 的内置图库应用程序获取/选取图像
我正在尝试从我的应用程序内部打开图库内置应用程序中的图像/图片。
我有图片的 URI(图片位于 SD 卡上)。
您有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在尝试从我的应用程序内部打开图库内置应用程序中的图像/图片。
我有图片的 URI(图片位于 SD 卡上)。
您有什么建议吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(20)
这是一个完整的解决方案。我刚刚使用@mad 下面的答案中提供的信息更新了此示例代码。另请查看 @Khobaib 的以下解决方案,解释如何处理 picasa 图像。
更新
我刚刚回顾了我原来的答案并创建了一个简单的 Android Studio 项目,您可以从 github 签出并直接导入到您的系统上。
https://github.com/hanscappelle/SO-2169649
(请注意,多个文件选择仍然需要工作)
单图片选择
由于用户疯狂,支持来自文件浏览器的图像。
选择多张图片
由于有人在评论中要求提供该信息,因此最好收集信息。
在意图上设置额外参数
EXTRA_ALLOW_MULTIPLE
:并在结果处理中检查该参数:
请注意,仅 API 级别 18+ 支持此功能。
This is a complete solution. I've just updated this example code with the information provided in the answer below by @mad. Also check the solution below from @Khobaib explaining how to deal with picasa images.
Update
I've just reviewed my original answer and created a simple Android Studio project you can checkout from github and import directly on your system.
https://github.com/hanscappelle/SO-2169649
(note that the multiple file selection still needs work)
Single Picture Selection
With support for images from file explorers thanks to user mad.
Selecting Multiple Pictures
Since someone requested that information in a comment and it's better to have information gathered.
Set an extra parameter
EXTRA_ALLOW_MULTIPLE
on the intent:And in the Result handling check for that parameter:
Note that this is only supported by API level 18+.
这是 hcpl 发布的精细代码的更新。但这也适用于 OI 文件管理器、astro 文件管理器和媒体库(已测试)。所以我想它适用于每个文件管理器(除了提到的之外还有很多其他的吗?)。对他编写的代码进行了一些更正。
Here is an update to the fine code that hcpl posted. but this works with OI file manager, astro file manager AND the media gallery too (tested). so i guess it will work with every file manager (are there many others than those mentioned?). did some corrections to the code he wrote.
hcpl 的方法在 KitKat 之前可以完美运行,但不能与 DocumentsProvider API 一起使用。为此,只需遵循文档提供程序的官方 Android 教程:https://developer.android.com/ Guide/topics/providers/document-provider.html ->打开文档,位图部分。
我简单地使用了 hcpl 的代码并对其进行了扩展:如果具有检索到的图像路径的文件抛出异常,我将调用此函数:
在 Nexus 5 上测试。
hcpl's methods work perfectly pre-KitKat, but not working with the DocumentsProvider API. For that just simply follow the official Android tutorial for documentproviders: https://developer.android.com/guide/topics/providers/document-provider.html -> open a document, Bitmap section.
Simply I used hcpl's code and extended it: if the file with the retrieved path to the image throws exception I call this function:
Tested on Nexus 5.
在上面的代码的基础上,我反映了如下的代码,可能它更合适:
basis with the above code, I reflected the code like below, may be it's more suitable:
我经历了@hcpl & 的解决方案; @疯狂的。 hcpl 的解决方案很好地支持图库中的本地图像和图像。 mad 在此之上提供了一个更好的解决方案 - 它也有助于加载 OI/Astro/Dropbox 图像。但在我的应用程序中,在处理现已集成到 Android Gallery 中的 picasa 库 时,两种解决方案都失败了。
我搜索了&分析了一下&最终有了更好的&克服这个限制的优雅解决方案。感谢 Dimitar Darazhanski 的博客,在这种情况下对我有帮助,我做了一些修改以使其更容易理解。这是我的解决方案 -
检查它并;如果有问题请告诉我。我已经测试过了&它在任何情况下都运作良好。
希望这对大家有帮助。
I went through the solution from @hcpl & @mad. hcpl's solution supports well for local image in the gallery & mad provided a better solution on top of that - it helps to load OI/Astro/Dropbox image as well. But in my app, while working on picasa library that's now integrated in Android Gallery, both solution fail.
I searched & analyzed a bit & eventually have come with a better & elegant solution that overcomes this limitation. Thanks to Dimitar Darazhanski for his blog, that helped me in this case, I modified a bit to make it easier to understand. Here is my solution goes -
Check it & let me know if there's some issue with it. I have tested it & it works well in every case.
Hope this will help everyone.
假设您的 SD 卡目录中有一个 image 文件夹,仅用于存储图像。
然后您可以决定如何处理活动中的内容。
这是一个检索图像路径名的示例,请使用您的代码对其进行测试,以确保您可以处理返回的结果。您可以根据需要更改代码以更好地满足您的需求。
我的建议是尝试让检索图像正常工作,我认为问题在于访问 SD 卡上图像的内容。看一下 在 SD 上显示图像卡。
如果您可以启动并运行它,可能通过提供正确的提供程序的示例,您应该能够找到代码的解决方法。
通过用您的进展更新此问题来让我了解最新情况。祝你好运
Assuming you have an image folder in your SD card directory for images only.
Then you can decide with what you would like to do with the content back in your activity.
This was an example to retrieve the path name for the image, test this with your code just to make sure you can handle the results coming back. You can change the code as needed to better fit your needs.
My advice is to try to get retrieving images working correctly, I think the problem is the content of accessing the images on the sdcard. Take a look at Displaying images on sd card.
If you can get that up and running, probably by the example supplying a correct provider, you should be able to figure out a work-around for your code.
Keep me updated by updating this question with your progress. Good luck
这是我对这个主题的重新审视,收集了这里的所有信息,以及其他相关的堆栈溢出问题。它从某些提供者返回图像,同时处理内存不足的情况和图像旋转。它支持图库、picasa 和文件管理器,如投递箱。用法很简单:构造函数接收内容解析器和 uri 作为输入。输出是最终的位图。
参考文献:
this is my revisit to this topic, gathering all the information here, plus from other relevant stack overflow questions. It returns images from some provider, while handling out-of-memory conditions and image rotation. It supports gallery, picasa and file managers, like drop box. Usage is simple: as input, the constructor receives the content resolver and the uri. The output is the final bitmap.
References:
这里有两个关于图像选择器的有用教程以及可下载的源代码:
如何创建 Android 图像选择器
如何在 Android 上选择和裁剪图像
但是,应用程序有时会被迫关闭,您可以通过将 android:configChanges 属性添加到 Manifest 文件的主活动中来修复它,如下所示:
看来相机 API 失去了方向控制,因此这将有所帮助。 :)
There are two useful tutorials about image picker with downloadable source code here:
How to Create Android Image Picker
How to Select and Crop Image on Android
However, the app will be forced to close sometime, you can fix it by adding android:configChanges attribute into main activity in Manifest file like as:
It seems that the camera API lost control with orientation so this will help it. :)
要显示图像和视频,请尝试以下操作:
To display images and videos try this:
第 1 步用于打开图库以选择图片的代码:
第 2 步< /strong> 在
onActivityResult
中获取数据的代码:Step 1 Code for opening the gallery to select pics:
Step 2 Code for getting data in
onActivityResult
:以防万一有帮助;我这样做是为了获取位图:
Just in case it helps; I do this to get the Bitmap:
以上答案都是正确的。我遇到了另一个问题,在 HTC M8 中,从图库中选择图像时,我的应用程序崩溃了。我得到的图像路径为空值。我使用以下解决方案进行了修复和优化。在 onActivityResult 方法中
}
Above Answers are correct. I faced an different issue where in HTC M8 my application crashes when selecting an image from gallery. I'm getting null value for image path. I fixed and optimized with the following solution. in onActivityResult method
}
检索特定类型的文件
此示例将获取图像的副本。
打开特定类型的文件
在 4.4 或更高版本上运行时,您请求打开由另一个应用程序管理的文件
原始来源
Retrieve a specific type of file
This example will get a copy of the image.
Open a specific type of file
When running on 4.4 or higher, you request to open a file that's managed by another app
Original source
除了之前的答案之外,如果您在获取正确路径(如 AndroZip)时遇到问题,您可以使用以下命令:
Additional to previous answers, if you are having problems with getting the right path(like AndroZip) you can use this:
请查找从图库中选择单张图像的答案
Please find the answer for the selecting single image from gallery
有一个更简单、更好的解决方案,使用 AndroidDevelopper 提供的 PhotoPicker。您可以选择一个或多个文件,并且可以选择媒体类型。
然后,
您可以在此处获取更多信息 PhotoPicker-AndroidDevelopers
there is a simpler and better solution using the PhotoPicker provided by AndroidDevelopper. You can select one file or multiple files and you can choose the media type.
And then,
You can get more information here PhotoPicker-AndroidDevelopers
从图库或相机打开图像的最快方法。
原始参考:以编程方式从 Android 中的图库获取图像
以下方法将从图库或相机接收图像并将其显示在 ImageView 中。所选图像将被存储在内部。
的代码
xml JAVA 类
Quickest way to open image from gallery or camera.
Original reference : get image from gallery in android programmatically
Following method will receive image from gallery or camera and will show it in an ImageView. Selected image will be stored internally.
code for xml
JAVA class
这是我的例子,可能与你的情况不完全一样。
假设您从 API 提供商处获取了 Base64 格式,为其指定文件名和文件扩展名,并将其保存到文件系统中的特定位置。
不要忘记首先在 AndroidManifest.xml 中设置适当的文件提供程序,
其中文件路径是 .../res/xml/file_path.xml 中的 xml
长话短说,首先准备好文件提供程序,传递 Uri对于已知且可访问的图片源,将其传递给 Intent,否则,将图片保存在所需位置,然后将该位置(作为 Uri)传递给 Intent。
here is my example, might not be as your case exactly.
assuming that you get base64 format from your API provider, give it a file name and file extension, save it to certain location in the file system.
Don't forget to set up a proper file provider at first place in AndroidManifest.xml
where the file path is a xml in .../res/xml/file_path.xml
Long story short, have file provider ready at first, pass Uri to Intent for known and accessible picture source, otherwise, save the picture in desired location and then pass the location (as Uri) to Intent.