Search code examples
c++cocos2d-x

Getting full path of file in COCOS2D-X on Android devices


I am trying to obtain an XML file in my project but I can't seem make work right. I am using libXML (the one that comes with cocos2d-x-2.0.4) to parse XML files.

I'm using CCFileUtils::sharedFileUtils() -> fullPathFromRelativePath( ); but the problem is, for Android versions, it will not give the full path. It works fine on iOS, though.

I then looked at the GitHub and saw something weird. It turns out that fullPathFromRelativePath( ) will only return whatever you pass onto it.

From the GitHub:

const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
    return pszRelativePath;
}

I've looked everywhere and all I get is how to read XML files using CCFileUtils. I am already able to parse XML files. The only issue is that I can't get the full path of the XML file using fullPathFromRelativePath() in Android.

How can I get the full path of the XML file in Android?


Solution

  • There is nothing wrong with that function. The problem is that your xml files are inside apk, which is a zipped file, you cannot read that file directly, you should use something like

    long tmpSize;
    const char* xmlData = CCFileUtils::sharedFileUtils()->getFileData(YOUR_PATH_TO_FILE, "r", &tmpSize);
    

    then you can use lib xml to handle the data you get.

    but remember you cannot modify anything inside apk file. if you want to write to xml, you need to copy it to some writable path first (like sdcard or using getWritablePath()).

    For the files not inside apk, you can use fopen() directly, you do not need getFileData() any more.

    Hope this helps.