如何拍摄照片并获取图像和缩略图? [英] How to capture photo and get image and thumbnail?

查看:68
本文介绍了如何拍摄照片并获取图像和缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信我已经为这个简单的问题苦苦挣扎了几个小时,但是我无法使其正常运行.原则上,我需要此功能:

I cannot believe that I've struggled with this simple problem for hours now, but I cannot get it to run properly. In principle I want this functionality:

用户单击一个按钮.触发 ACTION_IMAGE_CAPTURE 意向,并打开默认的相机应用程序.用户拍摄照片并返回到我的应用.我的应用程序显示缩略图.单击缩略图可打开图像查看器.

The user clicks a button. An ACTION_IMAGE_CAPTURE intent is fired and the default camera app opens. The user takes the photo and returns to my app. My app shows the thumbnail. Clicking the thumbnail opens the image viewer.

实际上非常简单.在本指南中,似乎只是复制和粘贴的问题: https://developer.android.com/training/camera/photobasics.html .但是魔鬼在细节上.

Actually pretty simple. And with this guide it seemed like it was a matter of copy and paste: https://developer.android.com/training/camera/photobasics.html. But the devil is in the details.

当前,它确实可以工作,但是性能却很糟糕,因为我正在动态创建缩略图(在我的实际应用中,我在 RecyclerView 中为每个条目显示了多个缩略图).我希望有一个简单的方法在搜索数十个Stack Overlow问题时被忽略了.

Currently, it does work, but performance is horrible because I create the thumbnails (in my actual app I show multiple thumbnails for each entry in a RecyclerView) on the fly. I hope there is a simple approach that I overlooked during searching through dozens of Stack Overlow questions.

理想情况下,我想触发我的 ACTION_IMAGE_CAPTURE 意向,让默认的相机应用生成缩略图,并将缩略图和图像存储在系统的内容提供程序中,以创建缩略图和图像,最终在 onActivityResult()中获得各自的content://URI.那太好了...

Ideally, I'd like to fire my ACTION_IMAGE_CAPTURE intent, have the default camera app generate the thumbnail, store the thumbnail and the image in the system's content providers for thumbnails and images and finally get the respective content:// URIs back in onActivityResult(). That would be so nice...

现在,这是我面临的问题:

Now, here are the problems I'm facing:

  • 我对 onActivityResult()中的结果意图调用 getData()时收到的URI始终为 null ,无论我是否给出 MediaStore.EXTRA_OUTPUT 值以及调用意图
  • 当省略 MediaStore.EXTRA_OUTPUT 时,我得到的缩略图是位图形式,而不是content://URI.因此,如果我不想在下次开始活动时生成缩略图,则必须自己保存缩略图.
  • 我不明白,如果我向相机传递一个content://URI来保存图像,为什么我没有得到缩略图.我的意思是,相机将图像加载到内存中,并且可以轻松生成缩略图.但是,相反,我必须再次从磁盘加载多个MB才能生成缩略图.即使以后保存缩略图以供进一步使用,这似乎也是一个巨大的浪费.
  • 我已阅读到某些相机应用程序确实确实会在任何情况下创建缩略图,并将其提供给图库等使用.但这似乎不是标准化的.
  • The URI I receive when invoking getData() on the resulting intent in onActivityResult() is always null whether or not I give a MediaStore.EXTRA_OUTPUT value along with the invoking intent
  • When omitting the MediaStore.EXTRA_OUTPUT extra I get the thumbnail in form of a bitmap and not as a content:// URI. So, I have to persist the thumbnail myself if I don't want to generate it next time I start my activity.
  • I don't understand why I don't get the thumbnail if I pass the camera a content:// URI to save the image to. I mean, the camera has the image loaded in memory and could easily generate the thumbnail. But instead, I have to load multiple MB from disk again just to generate the thumbnail. That seems like a huge waste even if I save the thumbnail for further usage afterwards.
  • I have read that some camera apps actually do create the thumbnail in any case and make it available to the gallery or something. But it seems not to be standardized.

因此,我想我希望两个content://URI根本不起作用.但是在我继续自己生成(最终重复)缩略图并将其保存并缓存到我的应用程序中之前,我想问一下:

So, I guess my wish two content:// URIs simply is not how it works. But before I proceed to generate (eventually duplicate) thumbnails myself and persist and cache them in my app I want to ask:

有一些我不知道的简单方法吗?是否可以同时检索图像和缩略图,还是必须自己生成缩略图?

注意:我想通过将Android应用程序所拍摄的照片添加到媒体数据库中,以使其对画廊可用.我无法想象每个访问该媒体数据库的应用程序(例如图库)都会创建自己的缩略图集,对吗?

Note: I want to make the photos taken with my app available to the gallery by letting Android add it to the media database. I can't imagine that every app that accesses that media database (like the gallery) creates it's own set of thumbnails, right?

提前谢谢!

推荐答案

无论我是否随调用意图一起提供MediaStore.EXTRA_OUTPUT值,在onActivityResult()的结果意图上调用getData()时收到的URI始终为空.

The URI I receive when invoking getData() on the resulting intent in onActivityResult() is always null whether or not I give a MediaStore.EXTRA_OUTPUT value along with the invoking intent

ACTION_IMAGE_CAPTURE 没有返回 Uri .如果您提供 EXTRA_OUTPUT ,则知道该图像应驻留在哪里,因此请在此处查找.

There is no Uri returned by ACTION_IMAGE_CAPTURE. If you supply EXTRA_OUTPUT, you know where the image is supposed to reside, so go look for it there.

省略MediaStore.EXTRA_OUTPUT时,我得到的缩略图是位图的形式,而不是content://URI.

When omitting the MediaStore.EXTRA_OUTPUT extra I get the thumbnail in form of a bitmap and not as a content:// URI.

正确.

我不明白,如果我向相机传递一个content://URI来保存图像,为什么我没有得到缩略图.

I don't understand why I don't get the thumbnail if I pass the camera a content:// URI to save the image to.

那不是 ACTION_IMAGE_CAPTURE 协议额外获得缩略图,而不能同时获得两者.

That's not the way the ACTION_IMAGE_CAPTURE protocol is documented. Either you get a full-size photo written to EXTRA_OUTPUT, or you get a thumbnail via the "data" extra, not both.

我的意思是,相机将图像加载到内存中,并且可以轻松生成缩略图.

I mean, the camera has the image loaded in memory and could easily generate the thumbnail.

没有单个相机".有数百种相机应用程序—既有预安装的,也有用户安装的—可能会在大约10,000个设备型号和大约20亿个设备中响应您的 ACTION_IMAGE_CAPTURE 请求.

There is no single "the camera". There are hundreds of camera apps — both pre-installed and user-installed — that might respond to your ACTION_IMAGE_CAPTURE request, across the ~10,000 device models and ~2 billion devices.

是否可以同时检索图像和缩略图

Is it possible to retrieve both, image and thumbnail

不是通过 ACTION_IMAGE_CAPTURE .

我必须自己生成缩略图吗?

do I have to generate the thumbnail myself?

MediaStore 索取缩略图.我没有玩过从 MediaStore 检索图像的操作,但是AFAIK它准备了索引图像的缩略图.但是,请记住,您的索引请求(例如 MediaScannerConnection.scanFile())是异步的.对于运行此应用程序期间捕获的图像,您可能需要临时自己管理缩略图,而对于所有其他图像,则依赖 MediaStore .

Ask the MediaStore for a thumbnail, perhaps. I haven't played with retrieving images from MediaStore, but AFAIK it prepares thumbnails of images that indexes. However, bear in mind that your request to index (e.g., MediaScannerConnection.scanFile()) is asynchronous. You may need to temporarily manage thumbnails yourself for images that you capture during this run of the app, relying on the MediaStore for all other images.

我无法想象每个访问该媒体数据库(例如图库)的应用程序都会创建自己的缩略图集

I can't imagine that every app that accesses that media database (like the gallery) creates it's own set of thumbnails

我想很多应用程序都需要全尺寸图像,并让其图像加载库(Glide,Picasso等)将图像缩放到所需大小.

I would imagine that many apps request the full-size images and let their image-loading library (Glide, Picasso, etc.) scale the image to the desired size.

谁想拍照仅获取缩略图?

Who would want to take a photo to only get the thumbnail?

ACTION_IMAGE_CAPTURE 协议已经存在了十年.在2008年,所有移动设备都非常昂贵:CPU,内存和带宽.即使在2018年,在世界许多地方,相对于收入水平,其中一些东西还是很昂贵的.仅仅因为想要全尺寸和缩略图图像并不意味着每个人都想要.

The ACTION_IMAGE_CAPTURE protocol has been around for a decade. In 2008, everything mobile was expensive: CPU, memory, and bandwidth. Even in 2018, in many parts of the world, some of that stuff is expensive, relative to income levels. Just because you want both full-size and thumbnail images does not mean that everybody does.

这篇关于如何拍摄照片并获取图像和缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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