Android Java Bindings , class field does not have the correct type.

Pedro_MonizPedro_Moniz Member ✭✭
edited April 2018 in Xamarin.Android

I have a lib with a class that has a field with byte[].
This field becomes IList</byte/>.
However I need this field to be used as input for methods in the library which have byte[] parameters since those did not get transformed into IList</byte/>.
I cannot do a to.Array Operation since it is too slow as I am handling images.

How can I force the field to be a byte[] ?
I have tried a ton of different things but nothing seems to work.

The fact that in some places I get a list and in others an array is killing me and I am wasting too much time which I currently do not have.

In the metadata I tried many combinations but they do not work.
Example:
attr path="(..)"/field[@name='buffer']" name="managedType">byte[] attr

Best Answers

  • Pedro_MonizPedro_Moniz ✭✭
    Accepted Answer

    I was able to work around this problem but the solution is probably on the side of the creator of the lib.

    Instead of having the variable public, he should have a getter for it.

Answers

  • Pedro_MonizPedro_Moniz Member ✭✭
    Accepted Answer

    I was able to work around this problem but the solution is probably on the side of the creator of the lib.

    Instead of having the variable public, he should have a getter for it.

  • I think we might be implementing the same library! I've hit exactly the same issue - could you let me know how you worked around it?

    (...and I agree a getter would be much better!)

  • tailsutailsu Member

    I posted a detailed workaround with an explanation, but the entire post got mysteriously deleted.

    Here's just the working function:

            public static unsafe byte[] ToByteArray(IList<byte> javaArray)
            {
                IList<byte> array = javaArray;
                var arrayHandle = ((IJavaObject)array).Handle;
                byte[] copy = new byte[array.Count];
                fixed (byte* p = copy)
                    JniEnvironment.Arrays.GetByteArrayRegion(new JniObjectReference(arrayHandle), 0, array.Count, (sbyte*)p);
                return copy;
            }
    
  • Pedro_MonizPedro_Moniz Member ✭✭

    I still see the detailed explanation and marked it.

    Thank you for your time and the detailed explanation.

Sign In or Register to comment.