react-native Android 7.0 FileUriExposedExceptionandroid.os.StrictMode in onFileUriExposed

import android.support.v4.content.FileProvider;  // FileProvider引入
import android.os.Build;  // Build引入
...
@ReactMethod
    public void install(String path) {
       
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        File file =new File(path);
        if(!file.exists()) {
            return;
        }
        // 判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "com.应用包名.fileprovider"即是在清单文件中配置的authorities,
            data = FileProvider.getUriForFile(_context, "com.应用包名.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            data = Uri.fromFile(file);
        }
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        _context.startActivity(intent);
    }
...

在AndroidManifest.xml 的 application添加

<manifest   xmlns:tools="http://schemas.android.com/tools">
...
        <provider
                tools:replace="android:authorities"
                android:name="android.support.v4.content.FileProvider"
                android:authorities="${applicationId}.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
            <meta-data
                    tools:replace="android:resource"
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths"
            />
        </provider>
...
</manifest>


android/src/main/res/xml添加file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
	<root-path name="root_path" path="." />
    <external-cache-path name="cache_download" path="download"/>
</paths>

注意:android 应用更新安装时出现《 文件包与具有同一名称的现有文件包冲突》可用上文${applicationId}解决
调试环境无效可在build.gradle文件新增debug配置

 buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        // 新增
        debug {
            signingConfig signingConfigs.release
        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值