从 ACTION_IMAGE_CAPTURE Intent 获取图像路径
您好,我正在使用 ACTION_IMAGE_CAPTURE
使用 Intent
捕获图像,如下所示:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT, (new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg"))
);
startActivityForResult(cameraIntent, 0);
我需要将图像存储在 SD 卡中,并使用 onActivityResult
检索该图像的路径代码>方法。我不知道如何获取当前捕获图像的图像路径。
如果有人知道请帮忙。
谢谢
Hi I am using ACTION_IMAGE_CAPTURE
for capturing image using Intent
as follows:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT, (new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg"))
);
startActivityForResult(cameraIntent, 0);
I need to store image in an sdcard and retrieve the path of that image using the onActivityResult
method. I don't know how to get the image path of the currently captured image.
If any one knows please help.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是它在 2.2 上的工作方式(与以前的版本不同)。当开始意图时,
您需要记住
mCapturedImageURI
。当您捕获图像时,在
onActivityResult()
中使用该 URI 来获取文件路径:更新: 在新的 Android 设备上,您不需要 MediaStore.EXTRA_OUTPUT,但您宁愿推断图像/video 来自 data.getData() 的 URI,从 onActivityResult(..., Intent data) 接收,如
Android ACTION_IMAGE_CAPTURE Intent
但是,由于这部分需要制造商进行调整,您可能仍然会遇到“旧”方法可能有用的手机。
This is how it works on 2.2 (different than on previous versions). When starting intent
you need to remember
mCapturedImageURI
.When you capture image, in
onActivityResult()
use that URI to obtain file path:UPDATE: On new Android devices you would not need MediaStore.EXTRA_OUTPUT, but you rather deduce image/video URI from data.getData() received from onActivityResult(..., Intent data), as nicely described under
Android ACTION_IMAGE_CAPTURE Intent
However, since this part is subject to manufacturer adaptation, you may still encounter phones where "old" approach may be useful.
另一种方法(在 android 2.1 上测试)是获取图库最后一张图像的 ID 或 AbsolutePath。
可以这样做:
并删除文件:
此代码基于帖子: 在拍摄意图照片后删除图库图像
Another way, tested on android 2.1, is take the ID or AbsolutePath of the gallery last image.
It can be done like that:
And to remove the file:
This code was based on the post: Deleting a gallery image after camera intent photo taken
这个问题很老了,但我已经和同样的问题斗争了半天了。问题是您的
ACTION_IMAGE_CAPTURE
Intent 将始终返回code=-1
和data=null
,除非您在您的应用程序中为应用程序设置以下权限AndroidManifest.xml
文件:如果您想从应用程序录制音频,还可以设置以下内容:
This question is very old but I have been battling with the same problem for half a day. The issue is that your
ACTION_IMAGE_CAPTURE
intent will always returncode=-1
anddata=null
unless you set the following permissions for the application in yourAndroidManifest.xml
file:You can also set the following if you want to record audio from your application:
删除
并添加
Remove
and add