ACTION_IMAGE_CAPTURE返回图像文件作为Extra而不是Data [英] 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也不告诉我多余的部分的名称
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()是因为我正在片段中使用此代码
这就是我用来获取相机的东西.
This is what I use to get the camera.
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字段中将一个小型图像作为Bitmap对象返回.这对于只需要较小图像的应用程序很有用.如果存在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
,例如我在
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返回图像文件作为Extra而不是Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!