Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

unity3d - Share Image on Android Application from Unity Game

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply call takeScreenShotAndShare() to take Screen Shot and share it. If you already have the image you want to share, just call StartCoroutine(shareScreenshot(path)); and pass in the path/location of the image. This only supports png images. To share jpeg, change

intentObject.Call<AndroidJavaObject>("setType", "image/png");

to

intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");

The whole code:

void takeScreenShotAndShare()
{
    StartCoroutine(takeScreenshotAndSave());
}

private IEnumerator takeScreenshotAndSave()
{
    string path = "";
    yield return new WaitForEndOfFrame();

    Texture2D screenImage = new Texture2D(Screen.width, Screen.height);

    //Get Image from screen
    screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    screenImage.Apply();

    //Convert to png
    byte[] imageBytes = screenImage.EncodeToPNG();


    System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/GameOverScreenShot");
    path = Application.persistentDataPath + "/GameOverScreenShot" + "/DiedScreenShot.png";
    System.IO.File.WriteAllBytes(path, imageBytes);

    StartCoroutine(shareScreenshot(path));
}

private IEnumerator shareScreenshot(string destination)
{
    string ShareSubject = "Picture Share";
    string shareLink = "Test Link" + "
http://stackoverflow.com/questions/36512784/share-image-on-android-application-from-unity-game";
    string textToShare = "Text To share";

    Debug.Log(destination);


    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"), textToShare + shareLink);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), ShareSubject);
        intentObject.Call<AndroidJavaObject>("setType", "image/png");
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        currentActivity.Call("startActivity", intentObject);
    }
    yield return null;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...