NativeShare不适用于Oreo(8.0或更高版本)

时间:2018-01-10 10:10:37

标签: c# unity3d android-8.0-oreo

我正在Unity3d上开发游戏并使用NativeShare脚本进行共享。并且该脚本完全适用于除具有OREO(8.0或更高版本)的设备之外的所有设备。当我在终端上调试我的设备时,我得到了这个异常" android.os.FileUriExposedException" 。请帮助我,以便我也可以在8.0或更高版本上运行此共享功能。提前谢谢。

NativeShare.cs如下:

公共类NativeShare:MonoBehaviour {

private static NativeShare instance;

public static NativeShare Instance {
    get {
        if(instance == null){
            instance = FindObjectOfType<NativeShare>();
        }
        return instance;
    }
    set {
        instance = value;
    }
}

void Awake(){
    instance = this;
}

public void ShareScreenshotWithText(string text)
{
    string ScreenshotName = Constants.GAME_NAME + " "+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
    string screenShotPath = Application.persistentDataPath + "/" + ScreenshotName;
    Application.CaptureScreenshot(ScreenshotName);

    Share(text,screenShotPath,"");
}

public void Share(string shareText, string imagePath, string url, string subject = "")
{
    #if UNITY_ANDROID
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
    AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + imagePath);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
    intentObject.Call<AndroidJavaObject>("setType", "image/png");

    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareText);

    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

    AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, subject);
    currentActivity.Call("startActivity", jChooser);
    #elif UNITY_IOS
    CallSocialShareAdvanced(shareText, subject, url, screenShotPath);
    #else
    Debug.Log("No sharing set up for this platform.");
    #endif
}

#if UNITY_IOS
public struct ConfigStruct
{
    public string title;
    public string message;
}

[DllImport ("__Internal")] private static extern void showAlertMessage(ref ConfigStruct conf);

public struct SocialSharingStruct
{
    public string text;
    public string url;
    public string image;
    public string subject;
}

[DllImport ("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);

public static void CallSocialShare(string title, string message)
{
    ConfigStruct conf = new ConfigStruct();
    conf.title  = title;
    conf.message = message;
    showAlertMessage(ref conf);
}

public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)
{
    SocialSharingStruct conf = new SocialSharingStruct();
    conf.text = defaultTxt; 
    conf.url = url;
    conf.image = img;
    conf.subject = subject;

    showSocialSharing(ref conf);
}
#endif

}

0 个答案:

没有答案