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 on Android!
Qt 6.11 is out! See what's new in the release blog

QFile on Android!

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    RenanHm
    wrote on last edited by
    #1

    Hello.. I'm trying to open a flat TXT file on Android.

    This way, with the file inside the program folder, I can read it:

        QFile file( "/data/user/0/com.digitalsof.pontofacilapp38/files/TEST.TXT" );
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
    

    But this way, with the file in the Documents folder I can't read:

     QFile file( "/storage/emulated/0/Documents/TEST.TXT" );
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
    

    Apparently it's an Android permissions issue.

    But how to grant permission to read in Documents folder??

    And the curious thing that I can save in the Documents folder. I just can't read.

    I use QT 5.14.2

    J.HilkJ 1 Reply Last reply
    0
    • R RenanHm

      Hello.. I'm trying to open a flat TXT file on Android.

      This way, with the file inside the program folder, I can read it:

          QFile file( "/data/user/0/com.digitalsof.pontofacilapp38/files/TEST.TXT" );
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
              return;
      

      But this way, with the file in the Documents folder I can't read:

       QFile file( "/storage/emulated/0/Documents/TEST.TXT" );
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
              return;
      

      Apparently it's an Android permissions issue.

      But how to grant permission to read in Documents folder??

      And the curious thing that I can save in the Documents folder. I just can't read.

      I use QT 5.14.2

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @RenanHm

      Check the answers here:

      https://forum.qt.io/topic/105406/how-to-save-files-in-android-in-downloads

      should not have changed since then


      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
      • R Offline
        R Offline
        RenanHm
        wrote on last edited by
        #3

        I perform the permissions check when opening the APP and before trying to open the file:

            using namespace QtAndroid;
            //SOPermission sop;
            QStringList permissions = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.INTERNET"};
            const QHash<QString, PermissionResult> results = requestPermissionsSync(permissions);
            auto ok = true;
            auto i = 0;
            while (ok && i< permissions.size()) {
                if (!results.contains(permissions[i]) || results[permissions[i]] == PermissionResult::Denied) {
                    qCritical() << "Couldn't get permission: " << permissions[i];
                    ok = false;
                    --i;
                }
                ++i;
            }
            return ok;
        

        But without success. The problem continues. Any idea?

        ekkescornerE 1 Reply Last reply
        0
        • R RenanHm

          I perform the permissions check when opening the APP and before trying to open the file:

              using namespace QtAndroid;
              //SOPermission sop;
              QStringList permissions = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.INTERNET"};
              const QHash<QString, PermissionResult> results = requestPermissionsSync(permissions);
              auto ok = true;
              auto i = 0;
              while (ok && i< permissions.size()) {
                  if (!results.contains(permissions[i]) || results[permissions[i]] == PermissionResult::Denied) {
                      qCritical() << "Couldn't get permission: " << permissions[i];
                      ok = false;
                      --i;
                  }
                  ++i;
              }
              return ok;
          

          But without success. The problem continues. Any idea?

          ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by ekkescorner
          #4

          @RenanHm probably you're dealing with SDK 30+?
          you can try FileDialog to see if you get access there.
          FileDialog should open the native Android FileDialog

          FileDialog
                  {
                      id: importFileDialog
                      title: "Select a file to import"
                      selectExisting: true
                      selectFolder: false
                      selectMultiple : false
                      folder: shortcuts.documents
                      onAccepted: 
                          console.log(myFileHelper.readFile(fileUrl))
                  }
          

          cpp:

              Q_INVOKABLE
              QString readFile(const QUrl &contentURL) const
              {
                  QFile file(contentURL.toString());
                  if(!file.open(QFile::ReadOnly |
                                QFile::Text))
                  {
                      return "Read failed";
                  }
                  QTextStream in(&file);
                  QString myText = in.readAll();
                  file.close();
                  return myText;
              }
          

          if you don't want to use FileDialog take a look at KDAB's SharedStorage: https://www.kdab.com/android-shared-storage-qt-wrapper/ and try out the example app if this library could be helpful.

          also I found out that QDir and QFile on Android not only accept filePathes, but also content URIs. (not documented: https://bugreports.qt.io/browse/QTBUG-99664)

          if all of this doesn't help - probably we have to wait until Qt has finalized support of Android (https://bugreports.qt.io/browse/QTBUG-98974) ScopedStorage.

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps

          1 Reply Last reply
          1

          • Login

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