Unity3d在android上共享图像

时间:2016-09-04 20:18:30

标签: android unity3d share classcastexception

尝试在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.access$1000(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时才会这样。我可能会遗漏一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

最后,这个问题与例外无关,外部应用程序对附加文件没有可见性,因为我将它们保存在内部应用程序存储中。

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