Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/platform/android/CCFileUtils-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ long FileUtilsAndroid::getFileSize(const std::string& filepath) const
std::vector<std::string> FileUtilsAndroid::listFiles(const std::string& dirPath) const
{

if(isAbsolutePath(dirPath)) return FileUtils::listFiles(dirPath);
if(!dirPath.empty() && dirPath[0] == '/') return FileUtils::listFiles(dirPath);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool FileUtilsAndroid::isAbsolutePath(const std::string& strPath) const
{
    DECLARE_GUARD;
    // On Android, there are two situations for full path.
    // 1) Files in APK, e.g. assets/path/path/file.png
    // 2) Files not in APK, e.g. /data/data/org.cocos2dx.hellocpp/cache/path/path/file.png, or /sdcard/path/path/file.png.
    // So these two situations need to be checked on Android.
    if (strPath[0] == '/' || strPath.find(_defaultResRootPath) == 0)
    {
        return true;
    }
    return false;
}

_defaultResRootPath is treated as 'absolute path'


std::vector<std::string> fileList;
string fullPath = fullPathForDirectory(dirPath);
Expand Down
18 changes: 16 additions & 2 deletions tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,10 +1408,24 @@ void TestListFiles::onEnter()
this->addChild(cntLabel);
cntLabel->setPosition(winSize.width / 2, winSize.height / 3);
// writeTest
auto list = FileUtils::getInstance()->listFiles("fonts");
std::vector<std::string> listFonts = FileUtils::getInstance()->listFiles("fonts");
auto defaultPath = FileUtils::getInstance()->getDefaultResourceRootPath();
std::vector<std::string> list = FileUtils::getInstance()->listFiles (defaultPath);

char cntBuffer[200] = { 0 };
snprintf(cntBuffer, 200, "%d", list.size());
snprintf(cntBuffer, 200, "'fonts/' %d, $defaultResourceRootPath %d",listFonts.size(), list.size());

for(int i=0;i<listFonts.size();i++)
{
CCLOG("fonts/ %d: \t %s", i, listFonts[i].c_str());
}

for(int i=0;i<list.size();i++)
{
CCLOG("defResRootPath %d: \t %s", i, list[i].c_str());
}


cntLabel->setString(cntBuffer);

}
Expand Down