Skip to content

Commit 1c69779

Browse files
gallipolyminggo
authored andcommitted
Fixed FileUtils::getFileSize() on android platform (#17632)
1 parent e3e0090 commit 1c69779

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

cocos/platform/android/CCFileUtils-android.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,32 @@ bool FileUtilsAndroid::isAbsolutePath(const std::string& strPath) const
251251
return false;
252252
}
253253

254+
long FileUtilsAndroid::getFileSize(const std::string& filepath)
255+
{
256+
long size = FileUtils::getFileSize(filepath);
257+
if (size != -1) {
258+
return size;
259+
}
260+
261+
if (FileUtilsAndroid::assetmanager)
262+
{
263+
string relativePath = filepath;
264+
if (filepath.find(_defaultResRootPath) == 0)
265+
{
266+
relativePath = filepath.substr(_defaultResRootPath.size());
267+
}
268+
269+
AAsset* asset = AAssetManager_open(FileUtilsAndroid::assetmanager, relativePath.data(), AASSET_MODE_UNKNOWN);
270+
if (asset)
271+
{
272+
size = AAsset_getLength(asset);
273+
AAsset_close(asset);
274+
}
275+
}
276+
277+
return size;
278+
}
279+
254280
FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, ResizableBuffer* buffer)
255281
{
256282
static const std::string apkprefix("assets/");

cocos/platform/android/CCFileUtils-android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class CC_DLL FileUtilsAndroid : public FileUtils
7070

7171
virtual std::string getWritablePath() const override;
7272
virtual bool isAbsolutePath(const std::string& strPath) const override;
73+
74+
virtual long getFileSize(const std::string& filepath) override;
7375

7476
private:
7577
virtual bool isFileExistInternal(const std::string& strFilePath) const override;

0 commit comments

Comments
 (0)