通过关闭严格模式绕过fileprovider 权限的控制的方法,绕过FileProvider在应用间共享文件的限制 7.0 开始,Android SDK 中的 StrictMode 策略禁止开发人员在应用外部公开 file:// URI。具体表现为,当我们在应用中使用包含 file:// URI 的 Intent 离开自己的应用时,程序会发生故障。(通过以下方式可用绕开) private void install(Context context, String filePath) { if (TextUtils.isEmpty(filePath)) { return; } try { File file = new File(filePath); Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); // 开启 } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // add more permission intent.setAction(Intent.ACTION_VIEW);//动作,查看 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");//设置类型 add more type // context.startService(intent); // context.sendBroadcast(intent); context.startActivity(intent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); builder.detectAll(); StrictMode.setVmPolicy(builder.build()); // 关闭 } } catch (Throwable e) { e.printStackTrace(); } } // 11-21 11:55:14.172 32330 32330 W System.err: android.os.FileUriExposedException: file:///storage/emulated/0/app-debug.apk exposed beyond app through Intent.getData() // 11-21 11:55:14.172 32330 32330 W System.err: at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978) // 11-21 11:55:14.173 32330 32330 W System.err: at android.net.Uri.checkFileUriExposed(Uri.java:2371) // 11-21 11:55:14.173 32330 32330 W System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:10247) // 11-21 11:55:14.173 32330 32330 W System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:10201) // 11-21 11:55:14.174 32330 32330 W System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667) // //
android 7.0 关闭严格模式绕过fileprovider共享文件的限制
最新推荐文章于 2023-07-13 20:07:50 发布