Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

c++ - Qt app permissions not granted on recent (>6) Android devices

Using Qt 5.14.2, with NDK r21b, I create this simple program:

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QDir>
#include <QStandardPaths>
#include <QFile>
#include <QTextStream>
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog dlg;

    dlg.setLayout( new QVBoxLayout() );

    QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

    if ( !QDir(path).exists() )
    {
        if ( QDir().mkdir(path) )
            dlg.layout()->addWidget( new QLabel( "Could create folder " + path, &dlg ) );
        else
            dlg.layout()->addWidget( new QLabel( "Could NOT create folder " + path, &dlg ) );
    }
    else
    {
        dlg.layout()->addWidget( new QLabel( "Folder exists " + path, &dlg ) );
    }

    path += "/from_qt.txt";

    QFile file(path);
    if (file.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&file);
        stream << "something" << endl;
        dlg.layout()->addWidget( new QLabel( "Could create file " + path, &dlg ) );
    }
    else
    {
        dlg.layout()->addWidget( new QLabel( "Could NOT create file " + path, &dlg ) );
    }

    dlg.show();
    return a.exec();
}

I add android.permission.WRITE_EXTERNAL_STORAGE to AndroidManifest.xml file.

  • Using Android 5, when installing the apk, the system reports the app needs to access storage and later the app will work (it reports Could NOT create...).
  • Using Android 6, when installing the apk, the system does not report the app needs any special access and later the app will work.
question from:https://stackoverflow.com/questions/65935980/qt-app-permissions-not-granted-on-recent-6-android-devices

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to https://developer.android.com/training/permissions/usage-notes:

Beginning with Android 6.0 (API level 23), users grant and revoke app permissions at run time, instead of doing so when they install the app. As a result, you'll have to test your app under a wider range of conditions. Prior to Android 6.0, you could reasonably assume that if your app is running at all, it has all the permissions it declares in the app manifest. Beginning with Android 6.0 the user can turn permissions on or off for any app, even an app that targets API level 22 or lower. You should test to ensure your app functions correctly whether or not it has any permissions.

So you need to add QT += androidextras in .pro file, include <QtAndroid> and add in the code:

auto  result = QtAndroid::checkPermission(QString("android.permission.WRITE_EXTERNAL_STORAGE"));
        if(result == QtAndroid::PermissionResult::Denied){
            QtAndroid::PermissionResultMap resultHash = QtAndroid::requestPermissionsSync(QStringList({"android.permission.WRITE_EXTERNAL_STORAGE"}));
            if(resultHash["android.permission.WRITE_EXTERNAL_STORAGE"] == QtAndroid::PermissionResult::Denied)
                return 1;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...