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. Android and iOS File Read and Write using QML
Forum Update on Monday, May 27th 2025

Android and iOS File Read and Write using QML

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 3.6k 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.
  • B Offline
    B Offline
    Bongani
    wrote on 29 Jun 2016, 12:52 last edited by Bongani
    #1

    I am developing a native mobile App for both android and iOS device, is it possible to create text files in the phone's internal storage and read from them using QML ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Jun 2016, 21:45 last edited by
      #2

      Hi,

      Purely on QML ? AFAIK, no. But you can provide your own C++ extension that will do it and use it from your QML code.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ekkescorner
        Qt Champions 2016
        wrote on 30 Jun 2016, 08:03 last edited by ekkescorner
        #3

        this works on android and iOS:

        bool ApplicationUI::checkDirs()
        {
            // Android: HomeLocation works, iOS: not writable
            // Android: AppDataLocation works out of the box, iOS you must create the DIR first !!
            mDataPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
            qDebug() << "Data Path: " << mDataPath;
            QDir myDir(mDataPath);
            if (!myDir.exists()) {
                bool ok = myDir.mkpath(mDataPath);
                if(!ok) {
                    qWarning() << "Couldn't create dir. " << mDataPath;
                    return false;
                }
                qDebug() << "created directory path" << mDataPath;
            }
            return true;
        }
        

        now you can read and write as usual

        QFile readFile(mDataPath+"/yourfile.txt");
        if(!readFile.exists()) {
                qDebug() << "file doesn't exist ";
                return false;
            }
            if (!readFile.open(QIODevice::ReadOnly)) {
                qWarning() << "Couldn't open file";
                return false;
            }
        

        took me some time to figure out that AppDataLocation already exists on Android, but must be created on iOS

        more infos:
        https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
        https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
        http://doc.qt.io/qt-5/qstandardpaths.html#standardLocations

        ekke ... Qt Champion 2016 | 2024 ... mobile business apps
        5.15 --> 6.8 https://t1p.de/ekkeChecklist
        QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

        1 Reply Last reply
        2
        • B Offline
          B Offline
          Bongani
          wrote on 26 Jul 2016, 10:01 last edited by
          #4

          Thanks @ekkescorner , your solution worked superbly. Much appreciated.

          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