Apk error in Android N installation
android.os.FileUriExposedException: file:///storage/emulated/0/download/1558506119385taiheApp.apk exposed beyond app through Intent.getData()
Because Android 7.0 added "private directory restricted access"
"Restricted access to private directory" means that in Android 7.0, in order to improve the security of private files, the private directory for Android N or later applications will be restricted access. This is similar to the sandbox mechanism of iOS.
To solve the problem:
Register the provider in the Android manifest.xml manifest file
<application ...> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.wlhl.dxcount" android:grantUriPermissions="true" android:exported="false"> <!--metadata--> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
- exported: the requirement must be false. If it is true, a security exception will be reported.
-
grantUriPermissions:true to grant temporary access to URI s
Limit.
Create a new xml folder in the res directory, and then create an xml file of provider path
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path path="Android/data/com.wlhl.dxcount/" name="files_root" /> <external-path path="." name="external_storage_root" /> </paths>
Then update the App and rewrite the code
public static void install(Context context) { File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) , "dx.apk"); Intent intent = new Intent(Intent.ACTION_VIEW); // Since the Activity is not started in the Activity environment, set the following label intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //Judge whether the version is above 7.0 File file= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"dx.apk"); //The context of parameter 1, the host address of parameter 2 Provider and the file shared by parameter 3 are consistent in the configuration file Uri apkUri = FileProvider.getUriForFile(context, "com.wlhl.dxcount", file); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); }else{ intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+enqueue+"dx.apk")), "application/vnd.android.package-archive"); } context.startActivity(intent); }