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. Copy file from assets to permanent location Qt + Android
Forum Updated to NodeBB v4.3 + New Features

Copy file from assets to permanent location Qt + Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 2 Posters 4.6k 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.
  • I Offline
    I Offline
    ifhmcos
    wrote on last edited by
    #1

    I'm trying to copy a default initialized database packaged in the assets folder to an permanent location in an android device. For this I have the following snippet of code:

    QString db_file = "/my_db";
    QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    QFile assets_path("assets:" + db_file);
    assets_path.copy(path + db_file);
    QFile::setPermissions(path + db_file, QFileDevice::ReadUser | 
                                          QFileDevice::WriteUser);
    

    And then I would go on to open the database in this new path, the thing is I'm getting the following error after the copy.

    Copy error "Cannot create /data/user/0/org.qtproject.Demo/files/my_db for output"
    

    What am I doing wrong?. Is also this the correct/proper way to do this? Can it be that it doesn't finds the asset file?

    Thanks.

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

      Hi and welcome to devnet,

      You should check if the copy is successful and print the error message returned by errorString if it fails.

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

        Sorry if I didn't make myself clear but that copy error is a print of errorString() right after the .copy() call.

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

          Ok, did you check whether that directory return by writableLocationwas already created ?

          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
          • I Offline
            I Offline
            ifhmcos
            wrote on last edited by
            #5

            No, I was assuming it was since the doc says "The returned path is never empty." Although I have to say I've tried with other QstandardPaths with no avail. Is there a way to see if I'm getting a permission error?

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

              From the documentation:

              QString QStandardPaths::writableLocation(StandardLocation type)
              
              Returns the directory where files of type should be written to, or an empty string if the location cannot be determined.
              
              Note: The storage location returned can be a directory that does not exist; i.e., it may need to be created by the system or the user.
              

              See the note, it's important.

              You can use QDir::exists to check whether the folder currently exists.

              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
              • I Offline
                I Offline
                ifhmcos
                wrote on last edited by ifhmcos
                #7

                Yes I've read that but the doc also said:

                Returns a directory location where persistent application data can be stored. This is an application-specific directory.
                To obtain a path to store data to be shared with other applications, use QStandardPaths::GenericDataLocation. 
                The returned path is never empty. On the Windows operating system, this returns the roaming path. This enum value was added in Qt 5.4.
                

                Anyways I'll check about the path and come back. Although I tried to create a folder I didn't know if I was doing it right, I tried this.

                QDir dir_folder(QDir::root());
                dir.mkdir(path);
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It means that the returned QString is not empty, not that the path it points to is not empty.

                  I would have used QDir::mkpath so it creates all the parent directories as needed.

                  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
                  • I Offline
                    I Offline
                    ifhmcos
                    wrote on last edited by ifhmcos
                    #9

                    Hi, tried what you suggested above but It still doesn't works. At first I though the asset wasn't being packade but QFile exists returns true, tried creating the folder with a good result but it just doesnt copies the file

                    Android debug output

                    // QFile file("assets:/my_db"); file.exists()
                    D LOG TAG : Does the asset exists asset? true
                    // QDir dir(QDir::root()); dir.mkpath(path); 
                    // QStandardPath::AppLocalData dir exists? after mkpath
                    D LOG TAG :   "/data/user/0/org.qtproject.Demo/files" true
                    // file.errorString() output
                    D LOG TAG : Err String: "Cannot create /data/user/0/org.qtproject.Demo/files/my_db for output"
                    

                    At this point i'm just mindf*. I checked everything it just doesn't copies the file :/

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ifhmcos
                      wrote on last edited by
                      #10

                      Well after googling a lot I found this https://bugreports.qt.io/browse/QTBUG-64103?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&showAll=true
                      Its working now with qt 5.9. Wish they had added that to known issues https://wiki.qt.io/Qt_5.10.0_Known_Issues.

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

                        Good catch !

                        I had forgotten about that one...

                        The known issues page has been updated.

                        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
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved