如何在 Qt for android 上获取应用程序参数

Posted

技术标签:

【中文标题】如何在 Qt for android 上获取应用程序参数【英文标题】:How to get application arguments on Qt for android 【发布时间】:2015-01-04 23:01:25 【问题描述】:

我有一个 Qt 应用程序,它在 androidManifest.xml 中定义了文件关联,所以当我在浏览器中选择一个文件时,我会得到一个关联应用程序的列表。我的应用程序在列表中,但是当我选择它时,文件路径未在我的main() 方法中的argv 列表中传递。路径是如何传递给应用程序的,我如何在 Qt/C++ 中拥有它?

【问题讨论】:

【参考方案1】:

经过一番研究,我得出了以下可行的解决方案:

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

【讨论】:

以上是关于如何在 Qt for android 上获取应用程序参数的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Qt API 在 android 中获取`/sdcard` 路径 [重复]

在 Qt Creator 中启动应用程序之前,如何将我在 Qt for Android 中的 .so 文件复制到 android-build/libs/arm64-v8a 中

Qt 5.9 for Android 实现沉浸式状态栏

Qt for Android修改应用程序的图标和名称

qt android 7 应该怎么加权限

在 Android 应用程序上部署 Qt 时出错