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. Failed to create QFile when deploying to Android device
Forum Updated to NodeBB v4.3 + New Features

Failed to create QFile when deploying to Android device

Scheduled Pinned Locked Moved Solved Mobile and Embedded
25 Posts 6 Posters 11.3k Views 2 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
    mvuori
    wrote on last edited by
    #2

    AFAIK, applicationDirPath() is not writeable, so you need to install any certificates by the deployment package, or store them somewere else (such as /sdcard/<your_app>)

    A 1 Reply Last reply
    2
    • M mvuori

      AFAIK, applicationDirPath() is not writeable, so you need to install any certificates by the deployment package, or store them somewere else (such as /sdcard/<your_app>)

      A Offline
      A Offline
      a_so
      wrote on last edited by
      #3

      @mvuori thank you for your reply!
      You mean path /data/local/tmp requires special privileges to write to it like in linux /usr/bin? Is that the reason why I can´t write to this path?
      Regarding the deployment package: How would you add the file? As external library? Under assets in the apk?
      I will definitely try to install the file to the sdcard on my phone.
      Do I need to add
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ?

      E 1 Reply Last reply
      0
      • A a_so

        @mvuori thank you for your reply!
        You mean path /data/local/tmp requires special privileges to write to it like in linux /usr/bin? Is that the reason why I can´t write to this path?
        Regarding the deployment package: How would you add the file? As external library? Under assets in the apk?
        I will definitely try to install the file to the sdcard on my phone.
        Do I need to add
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ?

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #4

        @a_so Android apps are sandboxed, you can use the whole file system only in a rooted device. See e.g. https://developer.android.com/guide/topics/data/data-storage.html#filesInternal. Basically you just should give no absolute path. I haven't tried creating files at all but I hope the given link guides you forward.

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

          Hi,

          QStandardPaths can be used to get writable locations.

          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
          2
          • A Offline
            A Offline
            a_so
            wrote on last edited by
            #6

            What I have tried so far is to add my certificate file under assets and with the resource system (qrc file) to the apk but on non-rooted device. While debugging it tells me that my file exists (as I added it) but that it is not callable.

            Consequently, I will definitely root my device (although I am afraid of crashing it) and give super user privileges to my app created with qt. Would then my file paths be writeable?
            I would also change the path for file creation to one outside of the apk. Has anyone done this so far?
            I will let you know about my progress.

            J.HilkJ 1 Reply Last reply
            0
            • A a_so

              What I have tried so far is to add my certificate file under assets and with the resource system (qrc file) to the apk but on non-rooted device. While debugging it tells me that my file exists (as I added it) but that it is not callable.

              Consequently, I will definitely root my device (although I am afraid of crashing it) and give super user privileges to my app created with qt. Would then my file paths be writeable?
              I would also change the path for file creation to one outside of the apk. Has anyone done this so far?
              I will let you know about my progress.

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

              @a_so hi,

              first of, @SGaist is right QStandardPath should solve your problem

              QString writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
              

              in writableLocation you should be able to read and write files as you please, even without permission from the manifest!

              Secendly, do you really need to save it as a file? If the certificate is only used inside your application, you can simply load it from your qrc-file


              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.

              A 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @a_so hi,

                first of, @SGaist is right QStandardPath should solve your problem

                QString writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                

                in writableLocation you should be able to read and write files as you please, even without permission from the manifest!

                Secendly, do you really need to save it as a file? If the certificate is only used inside your application, you can simply load it from your qrc-file

                A Offline
                A Offline
                a_so
                wrote on last edited by a_so
                #8

                @J.Hilk thank you for pointing that out to me. I read about it in the documentation and tried to use function "locate" with no success.

                I applied your code line and get the following directory:
                "/data/user/0/org.qtproject.example.sozius_client/files"

                Do I understand correctly that in this directory I can create/read/write files?
                If so, I would use this directory as the one to create my certificate file in or maybe push the certificate file with adb shell to this location.

                How do you load a file with qrc on Android?
                I tried this way:

                QFile file(":/icons/icons/192.168.0.7pem");

                ----edit----
                I tried to create my certificate file with

                QString certNameOnDisk = _hostName + "pem";
                QString writeableLocation;
                writeableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                QFile certFile(writeableLocation + QDir::separator() + certNameOnDisk, this);
                
                if(certFile.exists()) {
                    qWarning() << "cert file exists";
                }
                else
                {
                    qWarning() << "cert file does not exist";
                }
                
                

                Debugging tells me that "cert file does not exist" and in variable certFile it states "not callable - No symbol $QFile$ in current context. "

                ----edit----
                pushing the certfile to the directory of "writeableLocation" gives error

                [staff@vm-centos7 platform-tools]$ ./adb push /media/qt5-qwt6/so_socius/aso1_dbl1/software/qt/qt55/build/Linux/debug/certs/192.168.0.7pem /data/user/0/org.qtproject.example.sozius_client/files
                
                adb: error: failed to copy '/media/qt5-qwt6/so_socius/aso1_dbl1/software/qt/qt55/build/Linux/debug/certs/192.168.0.7pem' to '/data/user/0/org.qtproject.example.sozius_client/files/192.168.0.7pem': remote Permission denied
                /media/qt5-qwt6/so_socius/aso1_dbl1/so...ushed. 0.0 MB/s (1220 bytes in 0.053s)
                
                
                J.HilkJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Why do you need to write that file on disk ?

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

                  A 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Why do you need to write that file on disk ?

                    A Offline
                    A Offline
                    a_so
                    wrote on last edited by
                    #10

                    @SGaist Every time when my client connects to a different host (development board that runs Server application) or the hosts ip changes, there is a test of the SSL certificate implemented. As long as these parameters don't change I do not need to write a file. The certificate is then only compared to existing ones.

                    That is why I thought in the first place to add the certificate to qrc or assets then only a read operation would be needed.

                    1 Reply Last reply
                    0
                    • A a_so

                      @J.Hilk thank you for pointing that out to me. I read about it in the documentation and tried to use function "locate" with no success.

                      I applied your code line and get the following directory:
                      "/data/user/0/org.qtproject.example.sozius_client/files"

                      Do I understand correctly that in this directory I can create/read/write files?
                      If so, I would use this directory as the one to create my certificate file in or maybe push the certificate file with adb shell to this location.

                      How do you load a file with qrc on Android?
                      I tried this way:

                      QFile file(":/icons/icons/192.168.0.7pem");

                      ----edit----
                      I tried to create my certificate file with

                      QString certNameOnDisk = _hostName + "pem";
                      QString writeableLocation;
                      writeableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                      QFile certFile(writeableLocation + QDir::separator() + certNameOnDisk, this);
                      
                      if(certFile.exists()) {
                          qWarning() << "cert file exists";
                      }
                      else
                      {
                          qWarning() << "cert file does not exist";
                      }
                      
                      

                      Debugging tells me that "cert file does not exist" and in variable certFile it states "not callable - No symbol $QFile$ in current context. "

                      ----edit----
                      pushing the certfile to the directory of "writeableLocation" gives error

                      [staff@vm-centos7 platform-tools]$ ./adb push /media/qt5-qwt6/so_socius/aso1_dbl1/software/qt/qt55/build/Linux/debug/certs/192.168.0.7pem /data/user/0/org.qtproject.example.sozius_client/files
                      
                      adb: error: failed to copy '/media/qt5-qwt6/so_socius/aso1_dbl1/software/qt/qt55/build/Linux/debug/certs/192.168.0.7pem' to '/data/user/0/org.qtproject.example.sozius_client/files/192.168.0.7pem': remote Permission denied
                      /media/qt5-qwt6/so_socius/aso1_dbl1/so...ushed. 0.0 MB/s (1220 bytes in 0.053s)
                      
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @a_so said in Failed to create QFile when deploying to Android device:

                      QFile certFile

                      I believe simply writing

                      QFile f(filename);
                      

                      does not create a file, you'll have ton open the file

                      QFile f(filename);
                      if(f.open(QIODevice::readOnly))
                          f.close();
                      

                      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.

                      A 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @a_so said in Failed to create QFile when deploying to Android device:

                        QFile certFile

                        I believe simply writing

                        QFile f(filename);
                        

                        does not create a file, you'll have ton open the file

                        QFile f(filename);
                        if(f.open(QIODevice::readOnly))
                            f.close();
                        
                        A Offline
                        A Offline
                        a_so
                        wrote on last edited by a_so
                        #12

                        @J.Hilk I added the file.open() command in ReadOnly mode. But this does not help as

                        if(!file.exists())
                        request tells me there is no file under directory
                        /data/user/0/org.qtproject.example.sozius_client/files.

                        Is there a way that my App could access my SD card and manipulate or just read a file from it?

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

                          Do you mean you want to comparing the content of the certificate itself ?

                          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
                          • A a_so

                            @J.Hilk I added the file.open() command in ReadOnly mode. But this does not help as

                            if(!file.exists())
                            request tells me there is no file under directory
                            /data/user/0/org.qtproject.example.sozius_client/files.

                            Is there a way that my App could access my SD card and manipulate or just read a file from it?

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

                            @a_so "request tells me there is no file under directory" - I thought you want to create a file?
                            "Is there a way that my App could access my SD card and manipulate or just read a file from it?" - yes, using QFile, but you need to do it properly. You need to make sure your app has the rights to write there.

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

                            A 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @a_so "request tells me there is no file under directory" - I thought you want to create a file?
                              "Is there a way that my App could access my SD card and manipulate or just read a file from it?" - yes, using QFile, but you need to do it properly. You need to make sure your app has the rights to write there.

                              A Offline
                              A Offline
                              a_so
                              wrote on last edited by
                              #15

                              @jsulm Yes I want to create a file but as this did not work for me I tried to copy a file in the directory and just open and read it .

                              How do I give my app read/write permissions? I have in my AndroidManifest.xml permissions for "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE". I have a 8 GB SanDisk ultra SdCard in my Slot. Does the sdcard need to be formatted in a special way?

                              jsulmJ J.HilkJ 2 Replies Last reply
                              0
                              • A a_so

                                @jsulm Yes I want to create a file but as this did not work for me I tried to copy a file in the directory and just open and read it .

                                How do I give my app read/write permissions? I have in my AndroidManifest.xml permissions for "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE". I have a 8 GB SanDisk ultra SdCard in my Slot. Does the sdcard need to be formatted in a special way?

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

                                @a_so You wrote: "if(!file.exists())
                                request tells me there is no file under directory
                                /data/user/0/org.qtproject.example.sozius_client/files."
                                Which file do you expect to be where and who is writing this file to this directory?

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

                                A 1 Reply Last reply
                                0
                                • A a_so

                                  @jsulm Yes I want to create a file but as this did not work for me I tried to copy a file in the directory and just open and read it .

                                  How do I give my app read/write permissions? I have in my AndroidManifest.xml permissions for "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE". I have a 8 GB SanDisk ultra SdCard in my Slot. Does the sdcard need to be formatted in a special way?

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

                                  @a_so What API are you using?
                                  23+ Was kinda broken and you had to grand your app the requested permissions manually in the settings of your Hardwaredevice

                                  Chances are that this is adressed in Qt5.9. But I haven't tested that yet.


                                  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.

                                  A 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @a_so You wrote: "if(!file.exists())
                                    request tells me there is no file under directory
                                    /data/user/0/org.qtproject.example.sozius_client/files."
                                    Which file do you expect to be where and who is writing this file to this directory?

                                    A Offline
                                    A Offline
                                    a_so
                                    wrote on last edited by
                                    #18

                                    @jsulm hi, I tried to write a file to /data/user/0/org.qtproject.example.sozius_client/files as @J-Hilk said that by using QStandardPaths class should enable me to read/write to such a location even without permission from the manifest. Here is the relevant code part

                                    QString certNameOnDisk = _hostName + "pem";
                                    
                                    //Open the File
                                    QString writeableLocation;
                                    writeableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                                    QFile certFile(writeableLocation + QDir::separator() + certNameOnDisk, this);
                                    
                                             if(certFile.exists()) {
                                                qWarning() << "cert file exists";
                                            }
                                            else
                                            {
                                                qWarning() << "cert file does not exist";
                                            }
                                    
                                            certFile.open(QIODevice::ReadOnly);
                                            if(certFile.open(QIODevice::ReadOnly)) {
                                                qWarning() << "certFile is open";
                                                certFile.close();
                                            }
                                          
                                            if (!certFile.open(mode)) {
                                                qCritical() << "so_SslClient: Error reading or writing cert-File";
                                                return QSslCertificate();
                                            }
                                    
                                            //Write the file to disk
                                            if (mode == QIODevice::WriteOnly) {
                                                if (certFile.write(cert.toPem()) == cert.toPem().length()) {
                                                    qDebug() << "so_SslClient: New trusted certificate written with success";
                                                }
                                                return QSslCertificate();
                                            }
                                    
                                            //Read the file from disk
                                            QSslCertificate localCert = QSslCertificate(&certFile, QSsl::Pem);
                                            if (localCert.isNull()) {
                                                qWarning() << "so_SslClient: Certificate file empty";
                                            }
                                            return localCert;
                                    }
                                    

                                    The file should then be called e.g. 192.168.0.7pem and be located in /data/user/0/org.qtproject.example.sozius_client/files. My app, which is basically a client, should create this file.

                                    jsulmJ 2 Replies Last reply
                                    0
                                    • A a_so

                                      @jsulm hi, I tried to write a file to /data/user/0/org.qtproject.example.sozius_client/files as @J-Hilk said that by using QStandardPaths class should enable me to read/write to such a location even without permission from the manifest. Here is the relevant code part

                                      QString certNameOnDisk = _hostName + "pem";
                                      
                                      //Open the File
                                      QString writeableLocation;
                                      writeableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                                      QFile certFile(writeableLocation + QDir::separator() + certNameOnDisk, this);
                                      
                                               if(certFile.exists()) {
                                                  qWarning() << "cert file exists";
                                              }
                                              else
                                              {
                                                  qWarning() << "cert file does not exist";
                                              }
                                      
                                              certFile.open(QIODevice::ReadOnly);
                                              if(certFile.open(QIODevice::ReadOnly)) {
                                                  qWarning() << "certFile is open";
                                                  certFile.close();
                                              }
                                            
                                              if (!certFile.open(mode)) {
                                                  qCritical() << "so_SslClient: Error reading or writing cert-File";
                                                  return QSslCertificate();
                                              }
                                      
                                              //Write the file to disk
                                              if (mode == QIODevice::WriteOnly) {
                                                  if (certFile.write(cert.toPem()) == cert.toPem().length()) {
                                                      qDebug() << "so_SslClient: New trusted certificate written with success";
                                                  }
                                                  return QSslCertificate();
                                              }
                                      
                                              //Read the file from disk
                                              QSslCertificate localCert = QSslCertificate(&certFile, QSsl::Pem);
                                              if (localCert.isNull()) {
                                                  qWarning() << "so_SslClient: Certificate file empty";
                                              }
                                              return localCert;
                                      }
                                      

                                      The file should then be called e.g. 192.168.0.7pem and be located in /data/user/0/org.qtproject.example.sozius_client/files. My app, which is basically a client, should create this file.

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

                                      @a_so said in Failed to create QFile when deploying to Android device:

                                      QStandardPaths class should enable me to read/write to such a location even without permission from the manifest

                                      No, it should not - it does not give you any permissions to anything. It can tell you in which directories you have write access.

                                      Why do you open the file first in read only mode and then in "mode" mode (what ever mode is set to)?
                                      Did you verify that you can open the file in write mode?

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

                                      A 1 Reply Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @a_so What API are you using?
                                        23+ Was kinda broken and you had to grand your app the requested permissions manually in the settings of your Hardwaredevice

                                        Chances are that this is adressed in Qt5.9. But I haven't tested that yet.

                                        A Offline
                                        A Offline
                                        a_so
                                        wrote on last edited by a_so
                                        #20

                                        @J.Hilk I use API level 23 and Qt 5.8. I also tried level 21 and 22

                                        J.HilkJ 1 Reply Last reply
                                        0
                                        • A a_so

                                          @jsulm hi, I tried to write a file to /data/user/0/org.qtproject.example.sozius_client/files as @J-Hilk said that by using QStandardPaths class should enable me to read/write to such a location even without permission from the manifest. Here is the relevant code part

                                          QString certNameOnDisk = _hostName + "pem";
                                          
                                          //Open the File
                                          QString writeableLocation;
                                          writeableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                                          QFile certFile(writeableLocation + QDir::separator() + certNameOnDisk, this);
                                          
                                                   if(certFile.exists()) {
                                                      qWarning() << "cert file exists";
                                                  }
                                                  else
                                                  {
                                                      qWarning() << "cert file does not exist";
                                                  }
                                          
                                                  certFile.open(QIODevice::ReadOnly);
                                                  if(certFile.open(QIODevice::ReadOnly)) {
                                                      qWarning() << "certFile is open";
                                                      certFile.close();
                                                  }
                                                
                                                  if (!certFile.open(mode)) {
                                                      qCritical() << "so_SslClient: Error reading or writing cert-File";
                                                      return QSslCertificate();
                                                  }
                                          
                                                  //Write the file to disk
                                                  if (mode == QIODevice::WriteOnly) {
                                                      if (certFile.write(cert.toPem()) == cert.toPem().length()) {
                                                          qDebug() << "so_SslClient: New trusted certificate written with success";
                                                      }
                                                      return QSslCertificate();
                                                  }
                                          
                                                  //Read the file from disk
                                                  QSslCertificate localCert = QSslCertificate(&certFile, QSsl::Pem);
                                                  if (localCert.isNull()) {
                                                      qWarning() << "so_SslClient: Certificate file empty";
                                                  }
                                                  return localCert;
                                          }
                                          

                                          The file should then be called e.g. 192.168.0.7pem and be located in /data/user/0/org.qtproject.example.sozius_client/files. My app, which is basically a client, should create this file.

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

                                          @a_so said in Failed to create QFile when deploying to Android device:

                                          certFile.open(QIODevice::ReadOnly);
                                          if(certFile.open(QIODevice::ReadOnly)) {
                                          qWarning() << "certFile is open";
                                          certFile.close();
                                          }

                                          Why do you open the file twice in read only mode?!

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

                                          A 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