Android 7.0 camera encounters URI exposure error

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.