Share code not working on new Android Nougat (7.0)

Hello,

In my android game, I have a code to share best score with friends via messenger, twitter,… Code works fine on all Android versions except on new 7.0 Nougat. I don’t know how to figure out which line of code makes an error. Any ideas ?

    public void Share(){
        if (!(isProcessing)){
            StartCoroutine(ShareScreenshot());
        }
    }

    public IEnumerator ShareScreenshot(){
        isProcessing = true;
        yield return new WaitForEndOfFrame();
        Texture2D screenTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
        screenTexture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0);
        screenTexture.Apply();
        byte[] dataToSave = screenTexture.EncodeToPNG();
        string destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
        File.WriteAllBytes(destination, dataToSave);
        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>("setType", "image/jpeg");
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            currentActivity.Call("startActivity", intentObject);
        }
        isProcessing = false;
    }

An error happens somewhere inside an if statement, so it must be somewhere in these lines of code:

            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>("setType", "image/jpeg");
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            currentActivity.Call("startActivity", intentObject);

Display just freezes when share button is pressed.

Regards, L

Logcat giving no joy?

You could maybe find offending line by interleaving all lines with log out like:

            UnityEngine.Debug.LogWarning("DEBUG_LOG 0");
            AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
            UnityEngine.Debug.LogWarning("DEBUG_LOG1");
            AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
            UnityEngine.Debug.LogWarning("DEBUG_LOG2");
            intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
            UnityEngine.Debug.LogWarning("DEBUG_LOG3");
            AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
            UnityEngine.Debug.LogWarning("DEBUG_LOG4");
            AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + destination);
            UnityEngine.Debug.LogWarning("DEBUG_LOG5");
            intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
            UnityEngine.Debug.LogWarning("DEBUG_LOG6");
            intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
            UnityEngine.Debug.LogWarning("DEBUG_LOG7");
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            UnityEngine.Debug.LogWarning("DEBUG_LOG8");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            UnityEngine.Debug.LogWarning("DEBUG_LOG9");
            currentActivity.Call("startActivity", intentObject);
            UnityEngine.Debug.LogWarning("DEBUG_LOG10");

Filter your logcat output for “DEBUG_LOG”.
Find highest log you see in logcat before crash, next line is probably culprit.

Also, you should really check all your new instantiated stuff for null before proceeding, to have it fail and log out without crashing.

any solution ??