Error when using checkPermissions
-
Hi guys,
I'm developing an android app where I have to store some data and retrieve it when the app opens again. The problem is, I can't create a file in any place except the internal storage, but I need it in the external storage so that I can actually check the file and see what it contains.
I could narrow this down to be a permissions problem, so I tried create some code to request for permissions at the start and checked previous threads, but I got a very unexpected error.
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "helper.h" #include <QtAndroidExtras/QtAndroid> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); const QVector<QString> permissions({"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"}); for(const QString &permission : permissions){ auto result = QtAndroid::checkPermission(permission); if(result == QtAndroid::PermissionResult::Denied){ auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] == QtAndroid::PermissionResult::Denied) return -1; } } QQmlApplicationEngine engine; qmlRegisterType<Helper>("com.masternuts.helper", 1, 0, "Helper"); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
This is my main.cpp. What I'm getting is "undefined reference to 'QtAndroid::checkPermission(QString const&)'", and the same error for requestPermissionsSync.
I tried checking the definition of these functions and they do exist in the qtandroidextras module of my Qt installation. What could possibly be wrong here?
For extra information, I'm using Qt 5.12.3, building on Android arm64-v8a using qmake.
I know this could be a silly error, but I'm still not so familiar with how everything in Qt works. Any help would be appreciated.
Thanks in advance :)
-
@GangsterV said in Error when using checkPermissions:
I know this could be a silly error, but I'm still not so familiar with how everything in Qt works. Any help would be appreciated.
I don't know if it will resolve your problem, but
QtAndroid::checkPermission()
is only required for Android 6.0 (Marshmallow / API Level 23) and upper. For older Android version, the permisisons are set thought Manifest settings.You could add this check in your code:
if(QtAndroid::androidSdkVersion() >= 23) { const QVector<QString> permissions({"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"}); for(const QString &permission : permissions){ auto result = QtAndroid::checkPermission(permission); if(result == QtAndroid::PermissionResult::Denied){ auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] == QtAndroid::PermissionResult::Denied) return -1; } } }
-
@GangsterV said in Error when using checkPermissions:
What I'm getting is "undefined reference to 'QtAndroid::checkPermission(QString const&)'", and the same error for requestPermissionsSync.
Is that an error when compiling or when linking?
-
@GangsterV said in Error when using checkPermissions:
For extra information, I'm using Qt 5.12.3, building on Android arm64-v8a using qmake.
One other point, I've forgotten to ask: did you enable Android Extras in your PRO file (
QT+= androidextras
)?