Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] Android: How to get a byte array from java?

[SOLVED] Android: How to get a byte array from java?

Scheduled Pinned Locked Moved Mobile and Embedded
5 Posts 2 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    ltr6
    wrote on last edited by
    #1

    My java class returns a byte[] and I need to access it in qt.
    This is what I've tried.

    @
    QByteArray myQtClass::getArrayFromJava(QString id, QString name)
    {
    jstring jid = env->NewStringUTF(id.toStdString().c_str());
    jstring jname = env->NewStringUTF(name.toStdString().c_str());

     jbyteArray myArray = QAndroidJniObject::callStaticMethod<jbyteArray>("com/my/JavaClass", "getArray", "(Ljava/lang/String;Ljava/lang/String;)B]", jid, jname);
    

    // Convert to bytearray

    }
    @

    The following error is produced:

    error: undefined reference to '_jbyteArray* QAndroidJniObject::callStaticMethod<_jbyteArray*>(char const*, char const*, char const*, ...)'

    Is there a correct way to do it?

    The bytearray is a jpeg image. I have thought about converting it to a Base64 encoded string and then back, but that might be pretty expensive when dealing with a large number of files.

    Any help would be appreciated.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Iktwo
      wrote on last edited by
      #2

      I'm working on a Qt android launcher, here's how I do it

      @
      public static byte[] getApplicationIcon(String packageName) {
      ..
      return icon;
      }
      @

      @
      ..
      QAndroidJniObject applicationIcon = QAndroidJniObject::callStaticObjectMethod("com/iktwo/utils/QPackageManager",
      "getApplicationIcon",
      "(Ljava/lang/String;)[B", QAndroidJniObject::fromString(id).object<jstring>());

      if (!iconDataArray) {
      qDebug() << Q_FUNC_INFO << "No icon data";

          return image;
      }
      
      jsize iconSize = env->GetArrayLength(iconDataArray);
      
      if (iconSize > 0) {
          jbyte *icon = env->GetByteArrayElements(iconDataArray, false);
          image = QImage(QImage::fromData((uchar*) icon, iconSize,"PNG"));
          env->ReleaseByteArrayElements(iconDataArray, icon, 0);
      }
      

      ..
      @

      1 Reply Last reply
      0
      • L Offline
        L Offline
        ltr6
        wrote on last edited by
        #3

        Sorry, but I don't understand. You have a QAndroidJniObject called applicationIcon, which is supposed to be the bytearray? But then where does the iconDataArray come from? Did you leave out some conversion that happens in between from applicationIcon -> iconDataArray?

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Iktwo
          wrote on last edited by
          #4

          I'm sorry.. this is my function
          @
          QAndroidJniObject appIcon = QAndroidJniObject::callStaticObjectMethod("com/iktwo/utils/QPackageManager",
          "getApplicationIcon",
          "(Ljava/lang/String;)[B",
          QAndroidJniObject::fromString(id).object<jstring>());

          QAndroidJniEnvironment env;
          jbyteArray iconDataArray = appIcon.object<jbyteArray>();
          
          if (!iconDataArray) {
              qDebug() << Q_FUNC_INFO << "No icon data";
          
              return image;
          }
          
          jsize iconSize = env->GetArrayLength(iconDataArray);
          
          if (iconSize > 0) {
              jbyte *icon = env->GetByteArrayElements(iconDataArray, false);
              image = QImage(QImage::fromData((uchar*) icon, iconSize,"PNG"));
              env->ReleaseByteArrayElements(iconDataArray, icon, JNI_ABORT);
          }
          

          @

          1 Reply Last reply
          1
          • L Offline
            L Offline
            ltr6
            wrote on last edited by
            #5

            Thank you! It works.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved