如何在Unity游戏Android 7或更高版本中共享屏幕截图

时间:2018-10-25 16:51:36

标签: android unity3d

我想分享游戏中的ScreenShot或图像。在那我找到了适用于android 6或更低版本的代码,但不适用于android&或更高版本。它在日志中给出了例外

日志如下:

 AndroidJavaException: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.tiger.games/files/screenshot.png exposed beyond app through ClipData.Item.getUri()

这是我的代码,适用于android 6或更低版本,但不适用于android 7或更高版本。

    public GameObject CanvasShareObj;

    private bool isProcessing = false;
    private bool isFocus = false;

    public void ShareBtnPress()
    {
        if (!isProcessing)
        {
            CanvasShareObj.SetActive(true);
            StartCoroutine(ShareScreenshot());
        }
    }

    IEnumerator ShareScreenshot()
    {
        isProcessing = true;

        yield return new WaitForEndOfFrame();

        ScreenCapture.CaptureScreenshot("screenshot.png", 2);
        string destination = Path.Combine(Application.persistentDataPath, "screenshot.png");

        yield return new WaitForSecondsRealtime(0.3f);

        if (!Application.isEditor)
        {
            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://" + destination);
            intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"),
                uriObject);
            intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"),
                "Play Diceer it's amazing Board Game \n here is the link\n http://diceer.com");
            intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");


            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject chooser = intentClass.CallStatic<AndroidJavaObject>("createChooser",
                intentObject, "Share your new score");


            currentActivity.Call("startActivity", chooser);

            yield return new WaitForSecondsRealtime(1);
        }

        yield return new WaitUntil(() => isFocus);
        //CanvasShareObj.SetActive(false);
        isProcessing = false;
    }

0 个答案:

没有答案