Recently, the project has done, call the camera to take pictures to get the function.
Use the previous code directly and find that there is a problem with Android7.0.
After Android6.0, dynamic application permissions have become the norm.
Call camera to take pictures of this feature, you need to dynamically request camera permissions, SD card read and Write permissions.
However, when these permissions are added, the error will still be made.
Error: android.os.fileuriexposedexception:file:///storage/emulated. Exposed beyond app through Intent.getdata ()
This is a URI exposure error.
Starting with Android 7.0, when an app provides its own files for use by other apps, the app throws fileuriexposedexception if given a URI in file://format. This is because Google believes that the target app may not have file permissions and can cause potential problems. So let this act quickly fail.
There are two ways to solve this problem:fileprovider mode and vmpolicy mode.
The first is Google's official recommended solution, and I'm also using the first method here.
Step: 1. Add the following code to the Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package=""> <Application> <providerAndroid:name= "Android.support.v4.content.FileProvider"android:authorities= "${applicationid}.provider"android:exported= "false"android:granturipermissions= "true"> <Meta-dataAndroid:name= "Android.support.FILE_PROVIDER_PATHS"Android:resource= "@xml/provider_paths"/> </provider> </Application></Manifest>
View Code
2. Create a new XML folder in the Res directory and create a new provider_paths XML file
<? xml version= "1.0" encoding= "Utf-8" ?> < paths xmlns:android = "Http://schemas.android.com/apk/res/android" > <!-- "." Represents all paths --> < external-path name =" External_files " path =". " /> </ paths >
View Code
3. Code that uses the URI
Uri Photouri == fileprovider.geturiforfile (context, Context.getapplicationcontext (). Getpackagename () + ". Provider ", Createimagefile ());
View Code
After you modify the code, debug again and the program runs correctly.
PS: Just a small function, the Internet also has a lot of information, but it is better to summarize their own!
Android 7.0 camera encounters URI exposure error