问题
Trying to implement android image sharing on a Unity game, I've put together several sources, this is my current code.-
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
}
it looks ok to me, and share with...
dialog gets opened, but the image isn't appearing, and I'm seeing an error on android console.-
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)
I'm guessing that android is expecting EXTRA_STREAM
to be an ArrayList
, but as far as I know this is only true when using ACTION_SEND_MULTIPLE
. May I be missing something obvious here?
回答1:
Finally, the issue had nothing to do with the exception, external apps just had no visibility for attached files as I was saving them on internal app storage.
Enabling WriteAccess->External (SDCard)
on Unity Player settings
solved the issue.
来源:https://stackoverflow.com/questions/39321128/unity3d-share-image-on-android