Calling from C++ Java method which returns List<int[]>



  • Hi,

    I'm trying to call getSupportedPreviewFpsRange Java method from C++. This method has signature List<int[]> getSupportedPreviewFpsRange () so first I do:

    QJNIObjectPrivate rangeListNative = m_parameters.callObjectMethod("getSupportedPreviewFpsRange", "()Ljava/util/List;");
    

    which returns valid object. Then I try to call get method to access jintArray (?).

    Got undefined reference error:

    rangeListNative.callMethod<jintArray>("get", "(I)[I", i);
    

    Method not found error:

    rangeListNative.callObjectMethod<jintArray>("get", "(I)[I", i);
    

    Returns null pointer:

    rangeListNative.callObjectMethod("get", "(I)[I", i);
    

    What is the right way to do it?



  • I have a similar method signature that I use for Android. I have no idea if this will be beneficial for you or not...
    Possibly replace "(I)Ljava/lang/Object" with "(I)Ljava/lang/List"

    QAndroidJniObject jResult =resultsVector.callObjectMethod("get", "(I)Ljava/lang/Object;", i);
    


  • @Carmoneer but how can I get value for jintArray from it? is it a correct way:

    QJNIObjectPrivate jRange =resultsVector.callObjectMethod("get", "(I)Ljava/lang/Object;", i);
    jintArray jRange = static_cast<jintArray>(range.object());
    

    ?



  • Actually it works :) Here is a full code snippet:

    QJNIObjectPrivate rangeListNative = m_parameters.callObjectMethod("getSupportedPreviewFpsRange", "()Ljava/util/List;");
    int count = rangeListNative.callMethod<jint>("size");
    for (int i = 0; i < count; ++i) {
        QJNIObjectPrivate range = rangeListNative.callObjectMethod("get", "(I)Ljava/lang/Object;", i);
    
        jintArray jRange = static_cast<jintArray>(range.object());
        jint* rangeArray = env->GetIntArrayElements(jRange, 0);
    
        AndroidCamera::FpsRange fpsRange;
    
        /* int */ fpsRange.min = rangeArray[0];
        /* int */ fpsRange.max = rangeArray[1];
    
        env->ReleaseIntArrayElements(jRange, rangeArray, 0);
    
        rangeList << fpsRange;
    }
    

    wonder if it's correct (?).



  • Oh, great! Glad to hear it is working. :) Everything looks good to me, however, I am certainly no expert.


  • Qt Champions 2016

    @Ruslan-Baratov said:

    wonder if it's correct (?).

    I just use the "get" method. Not sure is your method "correct", but it looks fine.

    My code:
    https://github.com/benlau/quickandroid/blob/master/qasystemdispatcher.cpp#L41



  • I just use the "get" method

    @benlau I use "get" method too but only for Java object. As far as I understand element of the list is not a Java object, it's integer array int[].


  • Qt Champions 2016

    @Ruslan-Baratov said:

    I just use the "get" method

    @benlau I use "get" method too but only for Java object. As far as I understand element of the list is not a Java object, it's integer array int[].

    Everything in Java is an object. Regardless of the actual storage method , List<T> is in fact an array of java.lang.Object, but you could use it like an int[] .



  • Everything in Java is an object

    @benlau Okay, thanks for the information. Good to know :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.