将图像从内存分享到Android Unity游戏

时间:2016-10-08 02:08:19

标签: java unity3d java-native-interface share screenshot

此问题与之前回答的问题Share Image on Android Application from Unity Game

有关

我的问题是:

有没有办法在不使用文件的情况下共享图像?我想删除Android上的SD卡读/写权限。我不是Java程序员,所以我有时间让它工作。我的所有尝试都没有产生任何结果。

“程序员”使用以下代码回答了上一个问题,这对我来说很有用。我基本上想要使用变量imageBytes而不是文件。这甚至可能吗?

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" + "\nhttps://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;
}

Unity3d share image on android

1 个答案:

答案 0 :(得分:0)

您可以直接传递字节数组,无需将其转换为文件 - &gt; uri - &gt;串

以下是java代码,但您可以轻松将其转换为JNI包装器调用

Bundle bundle = new Bundle();
bundle.putByteArray("byteArr", imageBytes); //put byte array to bundle!!!
Intent intent = new Intent(...);
intent.putExtra(bundle );

要在另一个活动中检索图像字节数组,请使用以下代码:

getIntent().getByteArrayExtra("byteArr");

如果将上面的每个函数调用都包装到相应的JNI函数中,那将非常繁琐。另一方面,你可以:

  • 创建一个android库项目
  • 添加两个静态java函数,一个是saveByteArrayToBundle(...),另一个是getByteArrayFromBundle(...)
  • 将android库编译为aar文件;
  • 然后将aar文件放入Assets /文件夹(实际上任何子文件夹都可以)
  • 然后像在OP
  • 中那样调用两个静态java函数

如果您需要逐步完成,请参阅以下教程: