How can I get a float array from Android native library?

YupengJiaYupengJia Member ✭✭
edited December 2018 in Xamarin.Android

I have some native functions like this "extern "C" JNIEXPORT jfloatArray JNICALL Test(JNIEnv *env);"
And I invoke this function in C# by this code:

public static extern jfloatArray Test(IntPtr jnienv);

But my app throw an null pointer exception when I get the return value.
I have searched solutions for a long time, but none of those solutions is for getting jfloatArray.

It would be nice if you guys could help me solve this problem.

Tagged:

Best Answer

  • YupengJiaYupengJia ✭✭
    edited December 2018 Accepted Answer

    @YorkGo

    Thanks! But my point is to get float array from native library. I have looked into all official sample codes, but there is no single line about my issue. After trying thousands of solutions, I finally figured out what was going on. We can use mono C# Marshal, and My code is as the following:

    C++:

    extern "C" JNIEXPORT bool JNICALL Test(long long mataddress, long long timestamp, float* out);
    

    C#:

    [DllImport("xxxxxxx", EntryPoint = "Test")]
    private static extern bool GetPose(long mataddress, long timestamp, [In,Out] float[] pose);
    

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    To interop with native libraries in your Xamarin.Android project, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. Set the EntryPoint to specify the exact function to be called in the native code. eg:

    [DllImport ("sanangeles", EntryPoint = "Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause")]
    static extern void nativePause (IntPtr jnienv);
    

    Then, to call the native function, simply call the defined method.

    public override bool OnTouchEvent (MotionEvent evt)
    {
        if (evt.Action == MotionEventActions.Down)
        nativePause (IntPtr.Zero);
        return true;
    }
    

    Refer to Interop with Native Libraries to learn more about interoperating with native methods.

  • YupengJiaYupengJia Member ✭✭
    edited December 2018 Accepted Answer

    @YorkGo

    Thanks! But my point is to get float array from native library. I have looked into all official sample codes, but there is no single line about my issue. After trying thousands of solutions, I finally figured out what was going on. We can use mono C# Marshal, and My code is as the following:

    C++:

    extern "C" JNIEXPORT bool JNICALL Test(long long mataddress, long long timestamp, float* out);
    

    C#:

    [DllImport("xxxxxxx", EntryPoint = "Test")]
    private static extern bool GetPose(long mataddress, long timestamp, [In,Out] float[] pose);
    
  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited December 2018

    @YupengJia , glad to hear that you have resolved this issue, you could mark it as an answer, so that someone else who have encounter the problem can see the answer. :)

Sign In or Register to comment.