startActivity报错exposed beyond app through Intent.getData()

背景:

需要调用外部已安装的APP来打开文件,直接报错。

解决:

1、的application节的里面

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.xx.xxx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                />
        </provider>

2、file_apths;

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

3、打开比如docx

//android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent(Context mContext, String Path) {
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = FileProvider.getUriForFile(mContext, "com.xx.xxx.fileprovider", file);//Uri.fromFile(file);
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }

记得用startActivity开启哦。

不出什么意外的话,会弹出一个列表,选择打开文件。

道法自然
原文地址:https://www.cnblogs.com/jiduoduo/p/15080928.html