ACTION_IMAGE_CAPTURE返回图像文件作为Extra而不是Data
作者:互联网
我的应用程序的功能之一包括相机.用户拍照,然后应用程序显示图像.
我使用StartActivityForResult从ACTION_IMAGE_CAPTURE开始意图,并通过onActivityResult捕获响应.通常,我可以简单地在onActivityResult中收到的意图上调用getData()并获取MediaProvider的内容uri.
我的问题如下:我的测试设备之一,华为ALE-L21,我在onActivityResult中获得的意图没有数据,但是,它具有可分解的额外功能.我如何从中获取用户照片? Android Studio也没有告诉我多余的名称
免责声明:我之所以调用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中的代码
@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;
}
}
解决方法:
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.引用the documentation:
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,因此将获得带有缩略图的额外数据(getData().getExtra(“ data”)).
the intent I get in onActivityResult has no data, but instead, it has a parcellable extra
给定您的ACTION_IMAGE_CAPTURE请求,这就是您应该得到的.
如果需要全尺寸的图像,请包括EXTRA_OUTPUT,也许指向应用程序中的FileProvider,例如我在this sample app中演示的那样.然后,您就知道应该将照片放到哪里的Uri,因为您指定了Uri.
标签:android,android-intent,android-5-0-lollipop,camera,huawei 来源: https://codeday.me/bug/20191014/1912413.html