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