简体   繁体   中英

Using Androidjavaobject in C# Two-dimensional Arrays transfer to Java is possible? And returns too

I am working on a Unity3D & Android cross-platform project.

I want improve the performance, so I changed my code.

This is my first attempt.

In C#

string str = JO.Call<string> ("GetDevices");

in Java

public String GetDevices() {
      String devices = "";
      /* ... */
      return devices;
}

It works, but I don't like this.

So, I changed it like this:

In C#

string[,] str = new string[deviceNum,2];
str = JO.Call<string[,]> ("GetDevices");

In Java

public String[][] GetDevices() {
    String[][] devices = {{""}};
    /* ... */
    return devices;
}

But it doesn't work. What am I doing wrong?


This is my first attempt log :

 I/Unity: Exception: JNI: System.Array in n dimensions is not allowed
                                        at UnityEngine._AndroidJNIHelper.GetSignature (System.Object obj) [0x00000] in <filename unknown>:0 
                                        at UnityEngine._AndroidJNIHelper.GetSignature[String[,]] (System.Object[] args) [0x00000] in <filename unknown>:0 
                                        at UnityEngine._AndroidJNIHelper.GetMethodID[String[,]] (IntPtr jclass, System.String methodName, System.Object[] args, Boolean isStatic) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJNIHelper.GetMethodID[String[,]] (IntPtr jclass, System.String methodName, System.Object[] args, Boolean isStatic) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJavaObject._Call[String[,]] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJavaObject.Call[String[,]] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 

And I tried "Pef" way and log like this

 07-18 10:21:58.318 18999-19055/? I/Unity: Exception: JNI: Unknown generic array type 'System.String[]'
                                        at UnityEngine._AndroidJNIHelper.ConvertFromJNIArray[String[][]] (IntPtr array) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJNIHelper.ConvertFromJNIArray[String[][]] (IntPtr array) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJavaObject._Call[String[][]] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
                                        at UnityEngine.AndroidJavaObject.Call[String[][]] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 

You are using a multidimensional array in your C# code which is different from an array of arrays as you are using in your java code.

For more details on the difference look here: What are the differences between a multidimensional array and an array of arrays in C#?

You could try:

string[][] str = new string[2][];
str[0] = new string[deviceNum];
str[1] = new string[deviceNum];
str = JO.Call<string[][]> ("GetDevices");

And pay attention to the order of the array dimensions.

Finally, I solve this problem

in c#

 string[] devicesstr = new string[deviceNum*2];
 devicesstr = tcamJO.Call<string[]> ("GetDevices");

in java

 public String[] GetDevices() {
 String[] devices = {"",};
 /* ... */
     devices = new String[cameraIds.length * 2];
     int j =0;
     for (int i = 0; i < cameraIds.length + 1; i += 2) {
           devices[i] = cameraIds[j++];
           /* .... */
           if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
               devices[i+1] = "1";
           } else {
               devices[i+1] = "0";
           }
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM