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



  • 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.



  • 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);
    }
    

    ..
    @



  • 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?



  • 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);
    }
    

    @



  • Thank you! It works.


Log in to reply
 

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