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. Best way to get absolute file paths in Android?
QtWS25 Last Chance

Best way to get absolute file paths in Android?

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

    I want to open a file stored using the Android asset model and ifstream.

    I can detect and open files deployed this way using Qt e.g.

    @QImage("assets:/examples/myExample.txt")@

    but I can't open the same file using the equivalent std::ifstream method as it says file not found.

    Any ideas please? I'm guessing the STL method can't resolve the asset:\ path?

    I saw "this":http://stackoverflow.com/questions/7701801/obtaining-the-name-of-an-android-apk-using-c-and-the-nativeactivity-class method using a nativeActivity class but it seems over-the-top to have to use non-Qt / STL methods.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      I'm not very much used to the android system. But AFAIK, assets is a folder within the APK package and it is deployed as a folder where your application is deployed. So you need to get the exact path of the file which is in assets folder. It may be some levels up from the current dir.
      May be try this though,
      @
      QFileInfo file("assets:/examples/myExample.txt");
      file.absoluteFilePath();
      @
      Maybe this gives the exact path of the files and then you can pass it to std::ifstream.

      157

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yalnte
        wrote on last edited by
        #3

        Thanks - doesn't seem to work. Using Qt 5.1.1 on Android 4.3:
        @
        QFile myFile("assets:/examples/myExample.txt");
        qDebug("FILE NOT FOUND: %s",myFile.symLinkTarget().toStdString().c_str());
        QFileInfo myFile2("assets:/examples/myExample.txt");
        qDebug("FILE NOT FOUND: %s",myFile2.absoluteFilePath().toStdString().c_str());
        @

        gives:

        @
        FILE NOT FOUND:
        FILE NOT FOUND: assets:/examples/myExample.txt
        @

        Based on what is said "here":http://qt-project.org/doc/qt-5.1/qtdoc/platform-notes-android.html#assets-file-system and elsewhere does Qt have access to a virtual file system which is unpacking files from the APK each time?

        Is this something we can access through Qt? I'm not familiar with how to access this using the Android NDK but I guess that's one option.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Hmm. I think they must include this functionality to resolve the paths of the files in assets folder.

          But isn't it when the app is deployed there's asset folder deployed as it is within the app structure ? If yes then i guess you can access it going several levels up from the current path viz. for eg. "../../assets/test.png"

          157

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            you can try to this:
            @
            QFile file("assets:/examples/myExample.txt");
            ifstream(::_fdopen(file.handle(), "r"));
            @
            But i can't guarantee that it will work, but worth a shot.

            Edit: oh nvm...i've overread that it is not working with QFile..sry

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yalnte
              wrote on last edited by
              #6

              I've cheated and created a workaround:

              I copy the file from a location I know Qt can find to one I know the standard C++ code can find - in this case the Downloads area. Not sure how portable this is though!

              @QFile file("assets:/examples/myExample.txt");
              file.copy(QString("/storage/emulated/0/Download/myfile.txt"));
              @

              I can then use my standard C++ method and open it from there. Not a solution to be proud of, or particularly robust, but it seems to work for now.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dscyw
                wrote on last edited by
                #7

                I have encountered the same problem.How to access files on phone?

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

                  "A thread about accessing file on phone":https://qt-project.org/forums/viewthread/32904/

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    yalnte
                    wrote on last edited by
                    #9

                    This post here solved it for me:
                    https://qt-project.org/forums/viewthread/35519

                    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