Hello,

I am trying to get a list of audio track info from an Android device. On the Java side, this is accomplished with a static getMusic() method, which returns List<AudioTrack>. getMusic() lives in my custom AndroidDeviceMediaCollection class. AudioTrack is essentially a struct of various fields, with a toString() method. I am able to instantiate each AudioTrack that I expect to find, print it out in Java using toString(), and then add it to the list. But I am running into problems on the C++ side. In particular, it seems that I am not able to convert the returned List<AudioTrack>, which is held in a QAndroidJniObject, to a jobjectArray without destroying most of its contents! Here is the relevant C++ code:

Qt Code:
  1. GenericModel<Album>* AndroidDeviceMediaCollection::albumsModel() {
  2. QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
  3. jobject activity = (jobject) interface->nativeResourceForIntegration("QtActivity");
  4. QAndroidJniObject music = QAndroidJniObject::callStaticObjectMethod("org.qtproject.mythings.AndroidDeviceMediaCollection", "getMusic", "(Landroid/content/Context;)Ljava/util/List;", activity);
  5. Q_ASSERT_X(music.isValid(), "albumsModel", "Null QAndroidJniObject!");
  6. jobjectArray musicArray = music.object<jobjectArray>();
  7. QAndroidJniEnvironment qjniEnv;
  8. const int n = qjniEnv->GetArrayLength(musicArray);
  9. qDebug() << "Got jobjectArray of length:" << n; // prints expected value
  10. for (int i = 0; i < n; ++i) {
  11. jobject jAudioTrack = qjniEnv->GetObjectArrayElement(musicArray, i);
  12. if (jAudioTrack) {
  13. // parse, add to albums...
  14. qjniEnv->DeleteLocalRef(jAudioTrack);
  15. }
  16. else
  17. qDebug() << "Got null jAudioTrack at index:" << i; // most elements are null!
  18. }
  19. return &m_albums;
  20. }
To copy to clipboard, switch view to plain text mode 

To be clear, I am successfully converting to a jobjectArray with exactly the right number of elements, but for most of those elements I see the null debug message.

I was just wondering if someone could point out what I am doing wrong here, or if there is a different way to acquire and iterate over a list of instances of a custom type. Any insight appreciated. Thank you!