Android N Install apk error: android.os.FileUriExposedException

Source: Internet
Author: User

StackOverflow: http://stackoverflow.com/questions/38200282/ Android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

1. In the Androidmainifest.xml file, add:

<provider            android:name= "Android.support.v4.content.FileProvider"            android:authorities = "${applicationid}.provider"            android:exported= "false"            android:granturipermissions = "true" >            <meta-data                android:name= "Android.support.FILE_PROVIDER_PATHS"                 Android:resource= "@xml/provider_paths"/></provider>

###:

The package name of the Authorities:app. Fileprovider
Granturipermissions:true, which represents granting the URI temporary access permission
Exported:false
Resource: Next file to add

2. Create a new file named XML under Res, and then create a new file provider_paths:

<?xml version= "1.0" encoding= "Utf-8"?><paths>    <external-path        name= "Files_root"         Path= "android/data/com.example.downloadapk/"/>    <external-path        name= " External_storage_root "        path=". " /></paths>

###:

Path: Paths that require temporary authorization access (. Represents all Paths)
Name: Give the access path a name

3. Install the APK file:

   /*** Install apk file * *@paramdirectory where the Apkfile installation package is located*/    Private voidinstallapk (File apkfile) {Intent Intent=NewIntent (Intent.action_view); Try{string[] command= {"chmod", "777", apkfile.tostring ()}; Processbuilder Builder=Newprocessbuilder (command);        Builder.start (); } Catch(IOException ignored) {}if(Build.VERSION.SDK_INT >=Build.version_codes.            N) {intent.setflags (intent.flag_grant_read_uri_permission); Uri Contenturi=Fileprovider.geturiforfile (Getapplicationcontext (), buildconfig.application_id+ ". Fileprovider", Apkfile); Intent.setdataandtype (Contenturi,"Application/vnd.android.package-archive"); } Else{intent.setdataandtype (Uri.fromfile (apkfile),"Application/vnd.android.package-archive");        Intent.setflags (Intent.flag_activity_new_task);    } startactivity (Intent); }

Android N Install apk error: Android.os.FileUriExposedException

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.