内容uri使Android KitKat上的相机崩溃 [英] Content uri crashes camera on Android KitKat

查看:76
本文介绍了内容uri使Android KitKat上的相机崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android API 19(Kitkat)上使用内容uri和FileProvider时遇到问题.这是我用来在设备上打开相机并录制视频的代码:

I run into problem while using content uri and FileProvider on Android API 19 (Kitkat). Here's code that I use to open camera on device and record a video:

File file = new File(pathname);
Uri fileUri = FileProvider.getUriForFile(this, AUTHORITY_STRING, file);

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, requestCode);

我已经在API的23-24上测试了此代码,但效果很好,但是在API 19上,我开始在相机应用中拍摄视频后,相机关闭并显示RESULT_CANCELED. 当我尝试使用ACTION_IMAGE_CAPTURE动作拍照时,也会发生同样的事情. 我试图用Uri.fromFile()更改FileProvider.getUriForFile().这在Kitkat上完美运行,但是我不能在Android 7上使用它. 为什么Adnroid Kitkat上的Camera不想使用内容uri?

I've tested this code on API's 23-24 and it works just fine, but on API 19 camera closes with RESULT_CANCELED after I starting to take video in camera app. The same thing happening when I'm trying to take a picture with ACTION_IMAGE_CAPTURE action. I've tried to change FileProvider.getUriForFile() with Uri.fromFile(). This works perfectly on Kitkat, but I can't use it on Android 7. Why is Camera on Adnroid Kitkat doesn't want to work with content uri?

推荐答案

并非每个相机应用程序都支持EXTRA_OUTPUTcontent Uri值,尽管它们应该支持.但是,有成千上万个摄像头应用程序,并且某些百分比在这样的Uri上将失败.例如,直到今年夏天,Google自己的摄像头应用程序才不支持contentcontent UriACTION_VIDEO_CAPTUREEXTRA_OUTPUT.

Not every camera app will support content Uri values for EXTRA_OUTPUT, though they should. But, there are thousands of camera apps, and some percentage will fail on such a Uri. For example, until this summer, Google's own camera app did not support a content Uri for EXTRA_OUTPUT for ACTION_VIDEO_CAPTURE.

要么停止使用ACTION_VIDEO_CAPTURE,要么获得不可靠的结果.

Either stop using ACTION_VIDEO_CAPTURE or live with unreliable results.

如果希望继续使用file Uri值,请将targetSdkVersion降至23或更低,或者通过更改StrictMode配置禁用FileUriExposedException.

If you wish to try continuing to use file Uri values, drop your targetSdkVersion to 23 or lower, or disable the FileUriExposedException by changing the StrictMode configuration.

这篇关于内容uri使Android KitKat上的相机崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆