ACTION_IMAGE_CAPTURE 将图像文件作为额外而不是数据返回 [英] ACTION_IMAGE_CAPTURE returns imagefile as Extra instead of Data
问题描述
我的应用程序的一个功能包括相机.用户拍照,应用显示图像.
One feature of my app includes the camera. The user takes a picture and the app displays the image.
我使用 StartActivityForResult 从 ACTION_IMAGE_CAPTURE 开始一个意图,并使用 onActivityResult 捕获响应.通常我可以简单地根据我在 onActivityResult 中收到的意图调用 getData() 并获取 MediaProvider 的内容 uri.
I start an intent with ACTION_IMAGE_CAPTURE using StartActivityForResult and capture the response with onActivityResult. Usually I can simply call getData() on the intent I receive in onActivityResult and get the content uri for the MediaProvider.
我的问题如下:我的一个测试设备是华为 ALE-L21,我在 onActivityResult 中获得的意图没有数据,而是有一个可拆分的额外内容.我怎么能从中得到用户的照片?Android Studio 也没有告诉我这个 parcellable extra 的名称
My problem is the following: One one of my test devices, a Huawei ALE-L21, the intent I get in onActivityResult has no data, but instead, it has a parcellable extra. How could I get the users photo from that? Android Studio doesn't tell me the name of the parcellable extra either
免责声明:我调用 getActivity() 是因为我在片段中使用此代码
这是我用来拿相机的.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, getResources().getInteger(R.integer.RQ_PERMISSION_WRITE_EXTERNAL_STORAGE));
}
<小时>
这是我的 onActivityResult 中的代码
And here's the code in my onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
super.onActivityResult(requestCode, resultCode, resultIntent);
switch (requestCode) {
case 1: //RQ_ImageCapture
case 2: //RQ_ImageSelected
if (resultIntent != null) {
try {
Uri selectedImage = resultIntent.getData();
if(selectedImage != null) {
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
setImageAsBackground(selectedImage, bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
推荐答案
通常我可以简单地根据我在 onActivityResult 中收到的意图调用 getData() 并获取 MediaProvider 的内容 uri.
Usually I can simply call getData() on the intent I receive in onActivityResult and get the content uri for the MediaProvider.
您的代码在其中许多设备上都会失败,包括所有最近的 Nexus 设备.ACTION_IMAGE_CAPTURE
不应返回 Uri
.引用文档:
Your code will fail on many of them, including all recent Nexus devices. ACTION_IMAGE_CAPTURE
is not supposed to return a Uri
. Quoting the documentation:
如果 EXTRA_OUTPUT 不存在,则在额外字段中将小尺寸图像作为位图对象返回.这对于只需要小图像的应用程序很有用.如果存在 EXTRA_OUTPUT,则将全尺寸图像写入 EXTRA_OUTPUT 的 Uri 值.
If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
由于你没有包含 EXTRA_OUTPUT
,你会得到一个额外的 data
(getData().getExtra("data")
)缩略图.
Since you are not including EXTRA_OUTPUT
, you will get a data
extra (getData().getExtra("data")
) with a thumbnail image.
我在 onActivityResult 中获得的意图没有数据,而是有一个可拆分的额外内容
the intent I get in onActivityResult has no data, but instead, it has a parcellable extra
鉴于您的 ACTION_IMAGE_CAPTURE
请求,这就是您应该得到的.
Given your ACTION_IMAGE_CAPTURE
request, that is what you are supposed to get.
如果您想要全尺寸图片,请包含 EXTRA_OUTPUT
,可能指向您应用中的 FileProvider
,例如我在 这个示例应用.然后,您知道照片应该放在哪里的 Uri
,因为您指定了 Uri
.
If you want a full-size image, include EXTRA_OUTPUT
, perhaps pointing to a FileProvider
in your app, such as I demonstrate in this sample app. Then, you know the Uri
where the photo should go, because you specified that Uri
.
这篇关于ACTION_IMAGE_CAPTURE 将图像文件作为额外而不是数据返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!