Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qfile writing to file issue Android

Qfile writing to file issue Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 3 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m.kuncevicius
    wrote on last edited by
    #1

    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 = file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
            qDebug() << "open: " << open;
            if(open)
            {
                QTextStream out(&file);
                QDateTime time = QDateTime::currentDateTime();
                out << time.toString() + " qt tekstas" << endl ;
    
                file.close();
            }
            else
            {
                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"/>
    </manifest>
    

    Any suggestions how to solve this puzzle?

    J.HilkJ 1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on last edited by
      #2

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

      1 Reply Last reply
      3
      • M m.kuncevicius

        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 = file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
                qDebug() << "open: " << open;
                if(open)
                {
                    QTextStream out(&file);
                    QDateTime time = QDateTime::currentDateTime();
                    out << time.toString() + " qt tekstas" << endl ;
        
                    file.close();
                }
                else
                {
                    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"/>
        </manifest>
        

        Any suggestions how to solve this puzzle?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @m.kuncevicius
        @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

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • M Offline
          M Offline
          m.kuncevicius
          wrote on last edited by m.kuncevicius
          #4

          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...

          J.HilkJ 1 Reply Last reply
          0
          • M m.kuncevicius

            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...

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @m.kuncevicius
            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.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved