Android相机意图:如何获取全尺寸照片?
我正在使用意图启动相机:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParent().startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
并使用:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
photoImage.setImageBitmap(thumbnail);
photoImage.setVisibility(View.VISIBLE);
但它只是一个缩略图,如何获取完整的位图?我知道我可以使用自己的 Activity 并使用:
Camera.PictureCallback()
但是有没有办法使用 Intent 来做到这一点?
谢谢!
编辑:
我也尝试过:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
它适用于从图库中选择的照片,但对于相机意图,data.getData() 返回 null。
I am using intent to launch camera:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParent().startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
and using:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
photoImage.setImageBitmap(thumbnail);
photoImage.setVisibility(View.VISIBLE);
But it is only a thumbnail, how do I get the full bitmap? I know I can use my own Activity and use:
Camera.PictureCallback()
But is there anyway to do it using Intent?
Thanks!
edit:
I also tried:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
It works for photo selected from gallery, but for camera intent, data.getData() returns null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要获得全尺寸的相机图像,您应该将相机指向临时文件中保存图片,例如:
然后在图像捕获意图完成工作后 - 只需使用以下代码从
imageUri
抓取您的图片:PS< /strong> 需要针对 Android M 中应用的新安全限制修改代码 - 文件提供程序:
mImageUri
必须与FileProvider
一起打包To get full sized camera image you should point camera to save picture in temporary file, like:
Then after image capture intent finished to work - just grab your picture from
imageUri
using following code:P.S. Code need to be revised with respect to new security restriction applied in Android M - FileProvider:
mImageUri
has to be packed withFileProvider
打开相机并将图像保存到某个特定目录
处理图像
Open Camera and save image into some specific directory
Handle Image
尽管这是一个老问题并且有一个公认的答案,
我想分享我的解决方案。
在这种情况下,您不必创建临时文件。
此外,我们创建一个选择器,为用户提供以下两种服务:用相机拍照或从图库中选择现有的一张照片。
在这里我们检索结果:
Even though this is an old question and it has an accepted answer,
I would like to share my solution.
In this case you don't have to create a temporary file.
Additionally we creating a chooser which offers to user both: take a picture with the camera or pick an existing one from a gallery.
and here we retrieving results:
我还使用了 Vicky 的答案,但我必须将 uri 保存到一个包中,以避免在方向更改时丢失它。因此,如果您在倾斜设备后没有从意图中得到结果,可能是因为您的 uri 无法承受方向变化。
活动结果代码:
将 uri 保存到包中:
在创建期间从保存的包中检索它:
I also used the answer from Vicky but I had to save the uri to a bundle to avoid loss of it on orientation change. So if you don't get a result from your intent after tilting the device it might be because your uri did not survive the orientation change.
Activity Result code:
save the uri to the bundle:
retrieve it from your saved bundle during on create:
不要使用
onActivityResult
的数据。我花了很多时间来测试不同的解决方案。相机保存图片(即使您没有在 AndroidManifest 中设置相机和读卡权限),但随后onActivityResult
返回data == null
和MediaStore
返回错误的路径。在这些解决方案中,您只需获取最后一个画廊图像,而不是您的照片。Don't use
onActivityResult
's data. It took me many hours to test different solutions. A camera saves a picture (even if you don't set permissions for camera and card reading in AndroidManifest), but thenonActivityResult
returnsdata == null
andMediaStore
returns wrong path. In these solutions you simply get last gallery image, not your photo.好的,是时候还钱了。
因此,您在清单中拥有权限:
并且您还拥有提供程序元数据:
一个遗漏的细节是(android:authorities applicationId) - 您需要将其添加到您自己的应用程序包名称中。
因此,正如我们在
manifest
中提到的,您在res
文件夹下有xml
文件,并在其下创建了file_paths
:我们已经完成了复制粘贴第 1 部分。现在,在上面的活动中
onCreate(savedInstanceState: Bundle?)
定义了这些美丽的内容。您可能想像往常一样查看原始资源,但缺少详细信息:<一个href="https://developer.android.com/training/camera/photobasics" rel="nofollow noreferrer">链接
另一个缺失的细节是
packageName + ".fileprovider",
请注意,如果您已经有方法,则需要提供自己的包名称。createImageFile
函数测试。
使用 onClick 事件调用您的
dispatchTakePictureIntent()
方法,确保权限被允许不要检查数据,我们将通过 imagePath 获取它。如果您正在检查
Uri.parse(currentPhotoPath)
,请确保它是Uri.fromFile(File(currentPhotoPath))
现在您有了位图,有时间花其他时间/天如何调整解码大小,保存。
还有一种保存令牌图像的方法,也许你可以帮我看看我应该把它放在哪里,如果我需要将令牌图像保存在图库上
Ok time to pay back.
So you have your permissions in the manifest:
And you also have your provider metadata:
One missed detail is (android:authorities applicationId) - you need to add this to your own app package name.
So you have
xml
file underres
folder as we mentioned onmanifest
and under it u have createdfile_paths
with:We are done with copy paste part 1. Now on our activity above
onCreate(savedInstanceState: Bundle?)
define these beautiesYou might like to have a look at original resource but missing details as usual Android Developers : link
An other missing detail is
packageName + ".fileprovider",
be careful u need to give your own package name if u already have method.createImageFile
functionTesting.
Call your
dispatchTakePictureIntent()
method with onClick event, make sure permissions are permittedDon't check data we will get it through imagePath. If u are checking
Uri.parse(currentPhotoPath)
make sure it'sUri.fromFile(File(currentPhotoPath))
Now you have your bitmap, time to spend other hours/days how to resize decode, save.
There is also a method to save token image, maybe you could help me to see where should I put it, if I need token image to be save on gallery
API 级别 29
我尝试了接受的答案,但是在接受的答案中使用了
Environment.getExternalStorageDirectory()
,并且在后续答案在 API 级别 29 中已弃用。第三个答案中使用的MediaStore.Images.Media.DATA
在 API 级别中也已弃用29. 以下代码(Kotlin 中)将完整图像存储在MediaStore
中,如此 stackoverflow 答案 到另一个问题,并且不依赖于FileProvider
:API Level 29
I tried the accepted answer, but both
Environment.getExternalStorageDirectory()
used in the accepted answer andEnvironment.getExternalStoragePublicDirectory()
used in a subsequent answer are deprecated in API level 29. So is theMediaStore.Images.Media.DATA
used in the third answer deprecated in API level 29. The following code (in Kotlin) stores the full image inMediaStore
as shown in this stackoverflow answer to a different question and doesn't rely on theFileProvider
:要从相机捕获最大图片尺寸,我希望这些简单的步骤非常有用。
这里,从固定为要捕获的图片尺寸的最大尺寸中获取每个移动设备支持的尺寸。
To capture maximum picture size from camera, i hope these simple steps will be quite useful
Here, the supported size of the each mobile is taken, from that which size is maximum that is fixed as picture size to capture.