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

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

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

      @a_so api 23 is Android 6.0 Marshmallow, comes with the problems, you could try a lower one.

      Adjustment of your code sample, to what @jsulm said:

              if(certFile.exists()) {
                  qWarning() << "cert file exists";
              }
              else
              {
                  qWarning() << "cert file does not exist";
                  if(certFile.open(QIODevice::ReadOnly)) {
                          qWarning() << "certFile created";
                          certFile.close();
                  }else qWarning() << "certFIle could not be created";
              }
            
              if (!certFile.open(mode)) {
                  qCritical() << "so_SslClient: Error reading or writing cert-File";
                  return QSslCertificate();
              }
      

      edit:
      Also you return, without closing the file! You shouldn't do that...


      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
      1
      • jsulmJ jsulm

        @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?

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

        @jsulm this is the very original code. My client runs also on Linux and Windows. There the folder for certificates is created in the current directory of my project, where .pro and all header and source files are located.

        QSslCertificate so_SslClient::certFileOnDisk(const OpenMode mode, const QSslCertificate &cert)
        {
            //Get the current directory
            QString currentDir = QCoreApplication::applicationDirPath();
            QString certDir = currentDir + QDir::separator() + "certs";
        
            if (!QDir(certDir).exists()) {
                QDir(currentDir).mkdir("certs");
                qDebug() << "so_SslClient: No 'certs' folder in app-dir, created one";
            }
        
            //Save the file under the hostname + 'pem' file ending
            QString certNameOnDisk = _hostName + "pem";
        
            //Open the File
            QFile certFile(certDir + QDir::separator() + certNameOnDisk, this);
            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;
        }
          
        
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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?!

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

          @jsulm I am sorry. My mistake, not intentionally.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            a_so
            wrote on last edited by
            #25

            Guys thank you so much! The adjusted code from @J-Hilk did it for me + I used target SDK 21. Thank you for your help!

            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