Unity3d 在 android 上分享图像

Unity3d share image on android

尝试在 Unity 游戏上实现 android 图像共享,我整理了几个来源,这是我当前的代码。-

public static void shareImage(string subject, string title, string message, string imagePath) {
    #if UNITY_ANDROID

    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    intentObject.Call<AndroidJavaObject>("setType", "image/png");
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TITLE"), title);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), message);

    AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
    AndroidJavaClass fileClass = new AndroidJavaClass("java.io.File");

    AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + imagePath);

    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);

    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
    currentActivity.Call("startActivity", intentObject);

    #endif
}

我觉得没问题,share with... 对话框打开了,但图像没有出现,我在 android 控制台上看到一个错误。-

java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.util.ArrayList
    at android.os.Bundle.getParcelableArrayList(Bundle.java:805)
    at android.content.Intent.getParcelableArrayListExtra(Intent.java:5823)
    at com.whatsapp.ContactPicker.b(ContactPicker.java:93)
    at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:551)
    at android.app.Activity.performCreate(Activity.java:6500)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
    at android.app.ActivityThread.access00(ActivityThread.java:198)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6837)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

我猜测 android 期望 EXTRA_STREAM 成为 ArrayList,但据我所知,这仅在使用 ACTION_SEND_MULTIPLE 时才成立。我可以在这里遗漏一些明显的东西吗?

最后,这个问题与异常无关,外部应用程序无法看到附加文件,因为我将它们保存在内部应用程序存储中。

在 Unity Player settings 上启用 WriteAccess->External (SDCard) 解决了问题。