gpt4 book ai didi

c# - 如何在 Unity 中将 Uri.fromfile 转换为 FileProvider.getUriForFile?

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:00 28 4
gpt4 key购买 nike

我有来自 this 的代码统一回答代码以安装 apk,但它在 Android 7.0 中不起作用,因为不再支持 Uri.fromfile 并且现在应该使用 FileProvider.getUriForFile

我尝试在 android studio 中打开项目并按照本教程操作 list 文件 - https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

        AndroidJavaClass intentObj = new 
AndroidJavaClass("android.content.Intent");
string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW");
int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>
("FLAG_ACTIVITY_NEW_TASK");
AndroidJavaObject intent = new
AndroidJavaObject("android.content.Intent", ACTION_VIEW);

AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File",
apkPath);
AndroidJavaClass uriObj = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uri = uriObj.CallStatic<AndroidJavaObject>
("fromFile", fileObj);

intent.Call<AndroidJavaObject>("setDataAndType", uri,
"application/vnd.android.package-archive");
intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
intent.Call<AndroidJavaObject>("setClassName",
"com.android.packageinstaller",
"com.android.packageinstaller.PackageInstallerActivity");

AndroidJavaClass unityPlayer = new
AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity =
unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", intent);

最佳答案

只是替换

AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
AndroidJavaClass uriObj = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uri = uriObj.CallStatic<AndroidJavaObject>("fromFile", fileObj);

AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider");
AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);

authority 参数构造为:

string packageName = unityContext.Call<string>("getPackageName");
string authority = packageName + ".fileprovider";

然后在调用 currentActivity.Call 函数之前向 Intent 添加 FLAG_GRANT_READ_URI_PERMISSION 权限。

intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);

有关完整脚本,请参阅现在编辑的 How to install Android apk from unity application问题。

关于c# - 如何在 Unity 中将 Uri.fromfile 转换为 FileProvider.getUriForFile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com