Qt and Android Gallery - MediaStore using QAndroidJniObject



  • I am writing a code that needs to get an image path from Android Gallery. The code was written using QAndroidJniObject to write a equivalent java code in Qt

    My problem is that I didn't figure out how to get the Android MediaStore columns to return the image path. I have tried several ways with no success:

    QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;")

    QAndroidJniObject dadosAndroid = QAndroidJniObject::fromString("MediaColumns.DATA")

    QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "DATA", "Ljava/lang/String;")

    QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore.Images.Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;")

    QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore.Images.ImageColumns", "BUCKET_DISPLAY_NAME", "Landroid/net/Uri;")

    How can I do that? My complete code is below.

    void imagePickerAndroid::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
    {
        qDebug() << "Trabalha com os dados";
    
        jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
        if (receiverRequestCode == 101 && resultCode == RESULT_OK) {
    
            QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
    
            QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "DATA", "Ljava/lang/String;");
            
            QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
            
            QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
            
            cursor.callMethod<jboolean>("moveToFirst", "()Z");
    
            jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
            //columnIndex returns 0
    
            QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
    
        }
        else
        {
            qDebug() << "Caminho errado";
        }
    }


  • SOLVED

    Here is the code to get the full image path from android gallery using java equivalent code written with Qt:

    #include "imagepickerandroid.h"
    
    imagePickerAndroid::imagePickerAndroid()
    {
    
    }
    
    void imagePickerAndroid::buscaImagem()
    {
        QAndroidJniObject ACTION_PICK = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_PICK", "Ljava/lang/String;");
        QAndroidJniObject EXTERNAL_CONTENT_URI = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$Images$Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;");
    
        QAndroidJniObject intent=QAndroidJniObject("android/content/Intent", "(Ljava/lang/String;Landroid/net/Uri;)V", ACTION_PICK.object<jstring>(), EXTERNAL_CONTENT_URI.object<jobject>());
    
        if (ACTION_PICK.isValid() && intent.isValid())
        {
            intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("image/*").object<jstring>());
            QtAndroid::startActivity(intent.object<jobject>(), 101, this);
            qDebug() << "OK";
        }
        else
        {
            qDebug() << "ERRO";
        }
    }
    
    void imagePickerAndroid::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
    {
        jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
        if (receiverRequestCode == 101 && resultCode == RESULT_OK)
        {
            QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
            QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
            QAndroidJniEnvironment env;
            jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
            jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str());
            env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid);
            QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
            QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, NULL, NULL, NULL);
            jint columnIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
            cursor.callMethod<jboolean>("moveToFirst", "()Z");
            QAndroidJniObject resultado = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
            QString imagemCaminho = "file://" + resultado.toString();
            emit imagemCaminhoSignal(imagemCaminho);
        }
        else
        {
            qDebug() << "Caminho errado";
        }
    }

Log in to reply
 

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