1
\$\begingroup\$

I've captured screenshot in my game when player dies. I've following code to capture screen shot.

RenderTexture rt = new RenderTexture (800, 600, 24);
        MainCamera.targetTexture = rt;
        Texture2D texture = new Texture2D (800, 600, TextureFormat.RGB24, false);
        MainCamera.Render ();
        RenderTexture.active = rt;
        texture.ReadPixels (new Rect (0, 0, 800, 600), 0, 0);
        MainCamera.targetTexture = null;
        RenderTexture.active = null;
        Destroy (rt);
        byte[] bytes = texture.EncodeToPNG ();
        Directory.CreateDirectory (Application.persistentDataPath + "/GameOverScreenShot");
        File.WriteAllBytes (Application.persistentDataPath + "/GameOverScreenShot" + "/DiedScreenShot.png", bytes);

I am getting saved screenshot using following code.

byte[] bytes = File.ReadAllBytes (Application.persistentDataPath +"/GameOverScreenShot" + "/BirdDiedScreenShot.png");

    Texture2D texture = new Texture2D (800, 600, TextureFormat.RGB24, false);
    RectOffset tempOffset = new RectOffset (5, 5, 5, 5);
    texture.filterMode = FilterMode.Trilinear;
    texture.LoadImage (bytes);
    Sprite sprite = Sprite.Create (texture, new Rect (0, 0, 800, 400), new Vector2 (0.5f, 0.0f), 2.0f);
    ScreenShot_Image.GetComponent<Image> ().sprite = sprite;

Now, I want to share this screenshot on android application. As per my research i have got following code for that, but it is returning blank image.

//instantiate the class Intent
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");

    //instantiate the object Intent
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

    //call setAction setting ACTION_SEND as parameter
    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));

    //instantiate the class Uri
    AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");

    //instantiate the object Uri with the parse of the url's file
    string destination = Application.persistentDataPath + "/GameOverScreenShot" + "/DiedScreenShot.png";
    AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://"+destination);

    //call putExtra with the uri object of the file
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);

    //set the type of file
    intentObject.Call<AndroidJavaObject>("setType", "image/*");

    //instantiate the class UnityPlayer
    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

    //instantiate the object currentActivity
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

    //call the activity with our Intent
    currentActivity.Call("startActivity", intentObject);

What should I change in this?? Please help, Advance Thanks

\$\endgroup\$
0
\$\begingroup\$

Application.Persistant datapath stores your image inside of the apk, this is not what you want as your gallery will not see it. Before you call WriteAllBytes, use this code:

string myFolderLocation = "/storage/emulated/0/DCIM/Screenshots/";

            if (!System.IO.Directory.Exists(myFolderLocation))
            {
                System.IO.Directory.CreateDirectory(myFolderLocation);
            }
string myScreenshotLocation = myFolderLocation + "myFilename";

File.WriteAllBytes(myScreenshotLocation, bytes);

Then you refresh the android phone photo gallery, give it a minute before is actually is visible in the gallery, but you can see it inside that folder location as soon as you save the picture.

Note: mine works like a charm and its saving it on internal storage also.

\$\endgroup\$
-1
\$\begingroup\$
MainCamera.targetTexture = rt;
Texture2D texture = new Texture2D (800, 600, TextureFormat.RGB24, false);

//maybe you add this??

texture.apply();

MainCamera.Render ();
\$\endgroup\$
  • 5
    \$\begingroup\$ Please add some explanation as to how this will solve the problem. \$\endgroup\$ – John Hamilton Mar 6 '18 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.