如何在 Unity 中将 Uri.fromfile 转换为 FileProvider.getUriForFile?
How to convert from Uri.fromfile to FileProvider.getUriForFile in Unity?
我有一个代码来自 统一回答代码来安装 apk 但它在 Android 7.0 中不起作用,因为 Uri.fromfile
不再受支持并且 FileProvider.getUriForFile
现在应该被使用。
我尝试在 android studio 中打开项目并按照本教程操作清单文件 - 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);
有关完整的脚本,请参阅现在编辑的 问题。
我有一个代码来自 Uri.fromfile
不再受支持并且 FileProvider.getUriForFile
现在应该被使用。
我尝试在 android studio 中打开项目并按照本教程操作清单文件 - 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);
有关完整的脚本,请参阅现在编辑的