Important: Please read the Qt Code of Conduct -

Qfile writing to file issue Android

  • Hello, I want to write some information to .txt file. I am using this code:

    QString pathTo = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/qt.txt";
        qDebug() << pathTo;
        QFile file(pathTo);
            bool open = | QIODevice::Text | QIODevice::Append);
            qDebug() << "open: " << open;
                QTextStream out(&file);
                QDateTime time = QDateTime::currentDateTime();
                out << time.toString() + " qt tekstas" << endl ;
                qDebug() << "failed to open" << file.errorString() << file.error();

    And it works fine, but when I generate AndroidManifest.xml and other android files and try to run the code it fails with code: 5 ("Permission denied"). I don't forget to include permissions to AndroidManifest.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    Any suggestions how to solve this puzzle?

  • Hi,
    what "target SDK" are you using?
    Starting from 23, Android changed the "permission system" so you should use max 22.

  • Moderators

    @luca is right, the permission system changed for android Qt has not yet a nativ API to adress this on runtime.

    you have the following options:

    • use a lower API-lvl
    • use QStandardPaths::AppDataLocation <- does not requiere permission
    • use java-code to ask for storage permissions during runtime

  • Thank you @luca , @J-Hilk . I changed Target SDK to 22 and it worked. It sucks that Qt has no tools to ask permissions on runtime. So If I want to use camera or open for example gallery directory I need to use 22 SDK...

  • Moderators

    like I said, you'll have to modify QtActivety, the java wraper of qt, to ask for permissions before executing the app.

    Or use a lower api, like you said yourself.

    Last year my employer wanted to use the latest api so I was forced to go that route.
    The guys over at KDBA are aware of the issue, so we might get lucky and have something for it in Qt 5.9 or 5.10.

Log in to reply