小编els*_*sie的帖子

以编程方式在Android 7/api24中安装apk

我想让我的应用程序自动安装apk.这适用于api <24.但对于24岁,它失败了.Android实现了额外的安全性:

对于定位到Android 7.0的应用,Android框架会强制执行StrictMode API策略,该策略禁止在应用外部公开file:// URI.如果包含文件URI的intent离开了您的应用程序,则该应用程序将失败并出现FileUriExposedException异常.

所以我尝试了这个:

    Uri myuri;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
        myuri = Uri.parse("file://"+outapk);
    } else {
        File o = new File(outapk);
        myuri = FileProvider.getUriForFile(con, con.getApplicationContext().getPackageName() + ".provider", o);
    }
    Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(myuri,"application/vnd.android.package-archive");
    con.startActivity(promptInstall);
Run Code Online (Sandbox Code Playgroud)

但得到一个致命的例外:

com.android.packageinstaller "Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{b42ee8a 6826:com.android.packageinstaller/u0a15} (pid=6826, uid=10015) that is not exported from uid 10066". 
Run Code Online (Sandbox Code Playgroud)

我的清单中有export = true.

问题似乎是packageinstaller无法使用content:// uri.

有没有办法允许应用程序以编程方式安装带有api24的apk?

uri securityexception apk packageinstaller android-7.0-nougat

13
推荐指数
2
解决办法
6373
查看次数