java.lang.SecurityException: 权限拒绝:从ProcessRecord打开android.support.v4.content.FileProvider提供程序。

3
java.lang.SecurityException: 权限拒绝:从ProcessRecord打开提供程序android.support.v4.content.FileProvider。
<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="cn.cloudworkshop.miaoding.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                tools:replace="android:resource"/>
        </provider>

Java代码:

 @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            Intent install = new Intent(Intent.ACTION_VIEW);
            Uri contentUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                contentUri = FileProvider.getUriForFile(context, "cn.cloudworkshop.miaoding.fileprovider", file);
            } else {
                contentUri = Uri.fromFile(file);

            }
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.setDataAndType(contentUri, "application/vnd.android.package-archive");
            context.startActivity(install);
        }
    }

install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

这段代码会导致这样一个问题,如果我删除这段代码,安装后的应用程序将崩溃。


你在 XML 中设置了文件提供程序路径吗? - DevKRos
<paths> <root-path name="root" path="" /> <external-path name="external" path="" /> </paths> - skite
这个路径是空的吗?另外,你能发布崩溃的堆栈跟踪吗? - DevKRos
不,如果我删除“install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)”这行代码,它也可以被安装。 - skite
@DevKRos 安装完成后,安装结果页面没有出现。 - skite
你填写了“root” path="" /> <external-path name="external" path="" /> </paths> 部分或者是空的吗?其次,你是否获取了在外部存储器上写入的运行时权限? - DevKRos
1个回答

5

Baby~ 你可以尝试这种方式:

installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK)

我猜需要合并权限允许活动_new_task!

1
是的,这个可以工作,但是有人能解释一下他设置标志的方式有什么区别吗? - Renjith K N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接