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. Force saved file to have an extension on Android
Qt 6.11 is out! See what's new in the release blog

Force saved file to have an extension on Android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 4 Posters 984 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.
  • D Offline
    D Offline
    Donald Duck
    wrote on last edited by Donald Duck
    #1

    I'm creating an Android app that's supposed to save .txt files like this:

    QString chosenFile = QFileDialog::getSaveFileName(this, "Save", "", "Text files (*.txt)");
    if(!chosenFile.isEmpty()){    //If the user didn't press Cancel
        if(!chosenFile.contains(QRegularExpression("\\.txt$"))){    //If the chosen file name doesn't end with .txt
            chosenFile += ".txt";
        }
        QFile file(chosenFile);
        if(!file.open(QIODevice::WriteOnly)){
            qDebug() << "Something bad happened";
            return;
        }
        //Write the file contents
    }
    

    What this does is that it opens a save file dialog. If I enter a file name that ends with .txt, it saves the file without a problem, as expected.

    If I enter a file name that doesn't end with .txt, I would expect it to save the file and automatically add .txt at the end of the file name. But instead it creates an empty file with the file name I entered (without .txt) and prints Something bad happened in the console.

    Why does it do this and how do I fix it?

    jsulmJ 1 Reply Last reply
    0
    • D Donald Duck

      I'm creating an Android app that's supposed to save .txt files like this:

      QString chosenFile = QFileDialog::getSaveFileName(this, "Save", "", "Text files (*.txt)");
      if(!chosenFile.isEmpty()){    //If the user didn't press Cancel
          if(!chosenFile.contains(QRegularExpression("\\.txt$"))){    //If the chosen file name doesn't end with .txt
              chosenFile += ".txt";
          }
          QFile file(chosenFile);
          if(!file.open(QIODevice::WriteOnly)){
              qDebug() << "Something bad happened";
              return;
          }
          //Write the file contents
      }
      

      What this does is that it opens a save file dialog. If I enter a file name that ends with .txt, it saves the file without a problem, as expected.

      If I enter a file name that doesn't end with .txt, I would expect it to save the file and automatically add .txt at the end of the file name. But instead it creates an empty file with the file name I entered (without .txt) and prints Something bad happened in the console.

      Why does it do this and how do I fix it?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Donald-Duck Why don't you use https://doc.qt.io/qt-5/qstring.html#endsWith instead of using a regular expression? Did you check whether your if condition is actually hit? I mean: is "chosenFile += ".txt";" executed?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Donald-Duck Why don't you use https://doc.qt.io/qt-5/qstring.html#endsWith instead of using a regular expression? Did you check whether your if condition is actually hit? I mean: is "chosenFile += ".txt";" executed?

        D Offline
        D Offline
        Donald Duck
        wrote on last edited by
        #3

        @jsulm said in Force saved file to have an extension on Android:

        Did you check whether your if condition is actually hit? I mean: is "chosenFile += ".txt";" executed?

        Yes, I checked, and it was.

        Why don't you use https://doc.qt.io/qt-5/qstring.html#endsWith instead of using a regular expression?

        I just wasn't aware that it existed. Now that I am I can use it, but it doesn't solve the problem.

        jsulmJ 1 Reply Last reply
        0
        • D Donald Duck

          @jsulm said in Force saved file to have an extension on Android:

          Did you check whether your if condition is actually hit? I mean: is "chosenFile += ".txt";" executed?

          Yes, I checked, and it was.

          Why don't you use https://doc.qt.io/qt-5/qstring.html#endsWith instead of using a regular expression?

          I just wasn't aware that it existed. Now that I am I can use it, but it doesn't solve the problem.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Donald-Duck If you see "Something bad happened" then it means that open() failed. You should print out https://doc.qt.io/qt-5/qiodevice.html#errorString in this case to see why it failed. So, please add proper error handling to get more information...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Donald-Duck If you see "Something bad happened" then it means that open() failed. You should print out https://doc.qt.io/qt-5/qiodevice.html#errorString in this case to see why it failed. So, please add proper error handling to get more information...

            D Offline
            D Offline
            Donald Duck
            wrote on last edited by
            #5

            @jsulm file.errorString() just returns "Unknown error", which isn't very helpful.

            M 1 Reply Last reply
            0
            • D Donald Duck

              @jsulm file.errorString() just returns "Unknown error", which isn't very helpful.

              M Offline
              M Offline
              mvuori
              wrote on last edited by
              #6

              I would run the code in debugger and see what really happens to the file name and everything else. Perhaps the problem is before that code and the stack is corrupted or similar.

              1 Reply Last reply
              0
              • IntruderExcluderI Offline
                IntruderExcluderI Offline
                IntruderExcluder
                wrote on last edited by
                #7

                Note that on Android you cannot access files directly by absolute path if this path wasn't provided by file picker due to security reasons. So if file picker returned some/path/to/file you can operate only with that path, and some/path/to/file.txt will be another path which isn't provided as 'trusted'.

                This is true only for 'public' folders, like Downloads. You have full access to application's sandbox, if it can solve your problem.

                D 1 Reply Last reply
                3
                • IntruderExcluderI IntruderExcluder

                  Note that on Android you cannot access files directly by absolute path if this path wasn't provided by file picker due to security reasons. So if file picker returned some/path/to/file you can operate only with that path, and some/path/to/file.txt will be another path which isn't provided as 'trusted'.

                  This is true only for 'public' folders, like Downloads. You have full access to application's sandbox, if it can solve your problem.

                  D Offline
                  D Offline
                  Donald Duck
                  wrote on last edited by
                  #8

                  @IntruderExcluder Thanks, that explains why I get the error. Is there a way to get around it, for example by having the file picker add the extension automatically?

                  1 Reply Last reply
                  0
                  • IntruderExcluderI Offline
                    IntruderExcluderI Offline
                    IntruderExcluder
                    wrote on last edited by
                    #9

                    Not sure if it possible with Qt. I think you should dig into Android docs and make sure if it possible by doing with Java, then you prabably need to write some Java code and connect it via JNI. This might help you.

                    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