FileUriExposedException产生的原因

1.情况描述

targetVersion 7.0及以上的手机,Uri.fromFile()方法使用时,并通过startActivity等方式启动其他app时,会出现FileUriExposedException异常。

2.问题原因及解决方案

为了提高私有文件的安全性,Android框架执行StrictMode API政策,禁止在应用内公开file:// URL,如果一项包含文件URI的intent离开您的应用,会出现FileUriExposedException异常。

采用content:// Uri,并授予URI临时访问权限,使用FileProvider类即可。

FileProvider是contentProvider的子类,用于创建content:// Uri代替file:// Uri实现安全的应用间文件共享。

3.相关知识点

实际测试中发现,仅在startActivity时存在问题。sendBroadcast则不会产生此类问题。

Uri uri = Uri.fromFile(file);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

为什么呢?

Intent.prepareToLeaveProcess()方法中,作了说明。

/**
     * Prepare this {@link Intent} to leave an app process.
     *
     * @hide
     */
    public void prepareToLeaveProcess(boolean leavingPackage) {
        setAllowFds(false);

        if (mSelector != null) {
            mSelector.prepareToLeaveProcess(leavingPackage);
        }
        if (mClipData != null) {
            mClipData.prepareToLeaveProcess(leavingPackage, getFlags());
        }

        if (mExtras != null && !mExtras.isParcelled()) {
            final Object intent = mExtras.get(Intent.EXTRA_INTENT);
            if (intent instanceof Intent) {
                ((Intent) intent).prepareToLeaveProcess(leavingPackage);
            }
        }

        if (mAction != null && mData != null && StrictMode.vmFileUriExposureEnabled()
                && leavingPackage) {
            switch (mAction) {
                case ACTION_MEDIA_REMOVED:
                case ACTION_MEDIA_UNMOUNTED:
                case ACTION_MEDIA_CHECKING:
                case ACTION_MEDIA_NOFS:
                case ACTION_MEDIA_MOUNTED:
                case ACTION_MEDIA_SHARED:
                case ACTION_MEDIA_UNSHARED:
                case ACTION_MEDIA_BAD_REMOVAL:
                case ACTION_MEDIA_UNMOUNTABLE:
                case ACTION_MEDIA_EJECT:
                case ACTION_MEDIA_SCANNER_STARTED:
                case ACTION_MEDIA_SCANNER_FINISHED:
                case ACTION_MEDIA_SCANNER_SCAN_FILE:
                case ACTION_PACKAGE_NEEDS_VERIFICATION:
                case ACTION_PACKAGE_VERIFIED:
                    // Ignore legacy actions
                    break;
                default:
                    mData.checkFileUriExposed("Intent.getData()");
            }
        }

        if (mAction != null && mData != null && StrictMode.vmContentUriWithoutPermissionEnabled()
                && leavingPackage) {
            switch (mAction) {
                case ACTION_PROVIDER_CHANGED:
                    // Ignore actions that don't need to grant
                    break;
                default:
                    mData.checkContentUriWithoutPermission("Intent.getData()", getFlags());
            }
        }
    }

ACTION_MEDIA_SCANNER_STARTED 属于被忽略的action,不会进行checkFileUriExposed()判定。

4.参考文献

Google api:https://developer.android.google.cn/about/versions/nougat/android-7.0-changes.html?hl=zh-cn

实例:

https://www.cnblogs.com/kezhuang/p/8706988.html

https://www.kaelli.com/18.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值