Uga Buga
Uga Buga

Reputation: 1784

How to get application arguments on Qt for android

I have a Qt application which has file associations defined in the AndroidManifest.xml so when I select a file in my browser I get a list of associated applications. My application is in the list, but when I select it the file path is not passed in the argv list in my main() method. How is the path passed to the application and how can I have it in Qt/C++?

Upvotes: 1

Views: 947

Answers (1)

Uga Buga
Uga Buga

Reputation: 1784

After some research I came out with the following working solution:

void loadAndroidFile()
{
#ifdef Q_OS_ANDROID
    QAndroidJniObject activity = QtAndroid::androidActivity();
    if (activity.isValid()) {
        QAndroidJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
        if (intent.isValid()) {
            QAndroidJniObject data = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
            if (data.isValid()) {
                QAndroidJniObject path = data.callObjectMethod("getPath", "()Ljava/lang/String;");
                if (path.isValid())
                    // Here path.toString() returns the path of the input file
                    QMetaObject::invokeMethod(rootComponent, "setSourcePath", Q_ARG(QVariant, QVariant("file://" + path.toString())));
            }
        }
    }
#endif
}

Upvotes: 1

Related Questions