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. Read and write an txt file from android device
Forum Updated to NodeBB v4.3 + New Features

Read and write an txt file from android device

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 4 Posters 9.5k 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.
  • M Offline
    M Offline
    MathiasH
    wrote on last edited by
    #1

    hi,
    in my app I want to open an file, if it exists, read it and if it doesnt i want to create it. After reading i want to write new source into it.
    All my tries failed. I tried it with QFile.open but then the error "device not open" appears...
    Is there any other solution to do what I want?

    best wishes
    Mathias

    EDIT:
    btw what path do I have to use?
    "file://storage/sdcard/text.txt" or "/sdcard/text.txt" or only "text.txt" or is there any path connected to my app on the device?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should have a look at the QStandardPaths class, it will help you retrieve the folders that you can access from your application.

      Hope it helps

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

        Ok, the write-function works perfect but the read-function has got problems.
        The file exists on my device and the input.open(QIODevice::ReadWrite) gives no error but if i call readAll() the String is empty. seems like there is a failure.
        If I call input.open(QIODevice::ReadOnly) the errorString() returns "No such file or directory" although the file exists because I can write in it...

        Any other ideas?

        I think there is a problem with reading on a android device but dont know why :(

        1 Reply Last reply
        0
        • S Offline
          S Offline
          s.frings74
          wrote on last edited by
          #4

          Post a minimal example source.
          I doubt that there is an issue in Qt because the application would fail to start i it cannot access files atg all. Also the file I/O stuff is part of the Linux Kernel, there should be no difference between a Desktop Linux and Android.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MathiasH
            wrote on last edited by
            #5

            I dont know why but now after the 1000th try it works :/
            thanks for your help :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              s.frings74
              wrote on last edited by
              #6

              One working example:
              @
              #include <QStandardPaths>
              #include <QtGlobal>
              #include <QFile>
              #include <QDir>

              int main(int, char**)
              {
              QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
              qWarning("folder=%s",qPrintable(folder));

              QFile* file=new QFile&#40;folder+"/test.txt"&#41;;
              if (!file->exists()) {
                  // create the folder, if necessary
                  QDir* dir=new QDir(folder);
                  if (!dir->exists()) {
                      qWarning("creating new folder");
                      dir->mkpath(".");
                  }
                  qWarning("creating new file");
                  file->open(QIODevice::WriteOnly);
                  file->write("Hello World");
                  file->close();
              }
              if (file->exists()) {
                  qWarning("file exists");
                  file->open(QIODevice::ReadOnly);
                  QByteArray data=file->readAll();
                  qWarning("file data=%s",data.constData());
                  file->close();
              }
              

              }

              @

              1 Reply Last reply
              0
              • S Offline
                S Offline
                s.frings74
                wrote on last edited by
                #7

                Maybe the SD card was mounted by the computer and therefore detached from the Android OS ?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dpb1956
                  wrote on last edited by
                  #8

                  I'm having the exact same problem you had - trying to read a file on Android I know exists. I have an app that works perfectly on a Windows desktop. On Android I know it is being saved properly since I can read it immediately after saving it and the read works. I'm using the folder provided from QStandardPaths. After I exit the app and re-run it trying to read the same file results in a "File Not Found" message. I tried using the exact code given in another forum post that they said works and the same problem occurs. You said for some reason it started working after many tries. Do you have any idea why it started to work? Is there some kind of persistence flag that needs to be set?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dpb1956
                    wrote on last edited by
                    #9

                    I'm having the exact same problem you had - trying to read a file on Android I know exists. I have an app that works perfectly on a Windows desktop. On Android I know it is being saved properly since I can read it immediately after saving it and the read works. I'm using the folder provided from QStandardPaths. After I exit the app and re-run it trying to read the same file results in a "File Not Found" message. I tried using the exact code given in another forum post that they said works and the same problem occurs. You said for some reason it started working after many tries. Do you have any idea why it started to work? Is there some kind of persistence flag that needs to be set?

                    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