Qt 6.7 Android reading and opening files
-
I'm currently working on making a windows c++ program run on android. I've made it so it can save files correctly now, but the program isn't able to open or read content uris once I save and if I try to open a file. I'm wondering about the proper way to go about doing this, as I've noticed a lot of methods are outdated with new QT versions, and I've been going in circles and dead ends with my methods.
Do I need a runtime permission manager? Or is changing the AndroidManifest.xml enough?
Are java and jni classes necessary? Or is there another way to get this done?
Any methods or help would be greatly appreciated. Thank you
-
Hi, the way to read and write files on Android is the same such as Windows or Linux.
But pay attention: you can't read and write files where you want in Android.
The old Android versions such as Android 5, permit to read and write data in Documents, Download and home dir folders. The latest versions not.Now I use this code line
QDir Dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));to have a folder where I can read and write data.
-
I think Android 13 does not permit to access at home dir folders. I suggest you to try a file explorer example to verify which folders you can open.
Pay attantion, the manifest file is not enought to require persmissions. Some recents android Versions want that the permissions must be request on startup of the app.const QVector<QString> Permissions({"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_NETWORK_STATE", "android.permission.ACCESS_WIFI_STATE", "android.permission.INTERNET", "android.permission.NFC", "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}); for (const QString &Permission : Permissions) { switch(QtAndroid::checkPermission(Permission)) { case QtAndroid::PermissionResult::Denied: { qDebug() << Permission+ " requested"; QtAndroid::requestPermissionsSync(QStringList({Permission})); switch(QtAndroid::checkPermission(Permission)) { case QtAndroid::PermissionResult::Denied: qDebug() << Permission+ " denied"; break; case QtAndroid::PermissionResult::Granted: qDebug() << Permission+ " already granted"; break; } break; } case QtAndroid::PermissionResult::Granted: qDebug() << Permission+ " already granted"; break; } }
-
Thank you @mrdebug , I had the same except using QJniObject::callStaticMethod . I'm not sure if QtAndroid is available for 6.7 however? I tried looking into it and I may have to use QtAndroidPrivate using #include <QtCore/private/qandroidextras_p.h> for the permissions.
I am still wondering: do I need to use a java class in my project to deal with content uri? Thanks.
-
Hi, I'm still using Qt5 but I think Qt6 is the same.
I think it is non necessary to use java to read and write file or to relose an uri.
I think the Qt classes are enought.
I'm not so expert on Android. You can ask here
https://xdaforums.com