Best way to get absolute file paths in Android?



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


  • Moderators

    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.



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


  • Moderators

    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"


  • Moderators

    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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.