Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt and Android - How to launch camera by using Qandroidjniobject



  • I am new in Qt android app development. I want to open camera and take picture and get the path of image by using QtAndroid, and JNIObject.

    For example,
    Java code -

    Intent capture_image_intent = new Intent();
    capture_image_intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture_image_intent, 101);
    

    I written this java code in Qt by using Qandroidjniobject. This is given below,
    Qt code -

    QAndroidJniObject ACTION_CAPTURE = QAndroidJniObject::fromString("android.provider.MediaStore.ACTION_IMAGE_CAPTURE");
                QAndroidJniObject intent("android/content/Intent");
                if (ACTION_CAPTURE.isValid() && intent.isValid())
                {
                    intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_CAPTURE.object<jstring>());
                    QtAndroid::startActivity(intent.object<jobject>(), 101, this);
                    qDebug() << "OK";
                }
                else
                {
                    qDebug() << "ERROR";
                }
    

    Now, my question is by using above qt code my app cannot launch Camera. So, please help me for finding what is the problem?



  • I solved this above problem in Qt by using Qandroidjniobject.

    For launching camera by using intent code is given below,

    QAndroidJniObject ACTION_IMAGE_CAPTURE = 
       QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", 
       "ACTION_IMAGE_CAPTURE", "Ljava/lang/String;");
    QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", 
        ACTION_IMAGE_CAPTURE.object<jstring>());
    QtAndroid::startActivity(intent.object<jobject>(), 101, this);