android 7.0 关闭严格模式绕过fileprovider共享文件的限制

通过关闭严格模式绕过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)
//
//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值