最近在做一款cocos2d-x的游戏,想用access去判断文件是否存在,在windows和ios平台完全ok,但是android怎么都不可以。后来发现,原来anroid的资源文件都还在apk中未解压出来,cocos2d-x针对android时这样读取文件的:
1 unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
2 {
3 unsigned char * pData = 0;
4 string fullPath(pszFileName);
5
6 if ((! pszFileName) || (! pszMode))
7 {
8 return 0;
9 }
10
11 if (!m_obPreloadDirectory.empty()) {
12 do {
13 std::string filename = pszFileName;
14 size_t pos = filename.rfind("/");
15 if (pos != std::string::npos)
16 filename = filename.substr(pos + 1);
17 filename = m_obPreloadDirectory + filename;
18 FILE *fp = fopen(filename.c_str(), pszMode);
19 CC_BREAK_IF(!fp);
20
21 unsigned long size;
22 fseek(fp,0,SEEK_END);
23 size = ftell(fp);
24 fseek(fp,0,SEEK_SET);
25 pData = new unsigned char[size];
26 size = fread(pData,sizeof(unsigned char), size,fp);
27 fclose(fp);
28
29 if (pSize)
30 {
31 *pSize = size;
32 }
33
34 if (pData != NULL)
35 return pData;
36 } while (0);
37 }
38
39 if (pszFileName[0] != '/')
40 {
41 // read from apk
42 string pathWithoutDirectory = fullPath;
43
44 fullPath.insert(0, m_obDirectory.c_str());
45 fullPath.insert(0, "assets/");
46 pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize);
47
48 if (! pData && m_obDirectory.size() > 0)
49 {
50 // search from root
51 pathWithoutDirectory.insert(0, "assets/");
52 pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), pathWithoutDirectory.c_str(), pSize);
53 }
54 }
55 else
56 {
57 do
58 {
59 // read rrom other path than user set it
60 FILE *fp = fopen(pszFileName, pszMode);
61 CC_BREAK_IF(!fp);
62
63 unsigned long size;
64 fseek(fp,0,SEEK_END);
65 size = ftell(fp);
66 fseek(fp,0,SEEK_SET);
67 pData = new unsigned char[size];
68 size = fread(pData,sizeof(unsigned char), size,fp);
69 fclose(fp);
70
71 if (pSize)
72 {
73 *pSize = size;
74 }
75 } while (0);
76 }
77
78 /*if (! pData && isPopupNotify())
79 {
80 std::string title = "Notification";
81 std::string msg = "Get data from file(";
82 msg.append(fullPath.c_str()).append(") failed!");
83 CCMessageBox(msg.c_str(), title.c_str());
84 }*/
85
86 return pData;
87 }这个方法在cocos2d-x源码platform/android的CCFileUtils类中,我们可以看到pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), pathWithoutDirectory.c_str(), pSize)这样一段代码,其实它是直接去获取apk包里面的资源,所以你access就行不通了。
有两种方式可以解决这个问题:
1、直接判断CCFileUtils::getFileData返回值是否为NULL,不过这个方式有一个问题,当你同一个页面判断的资源很多的话,这个是很耗时的,因为它每次判断都要去解压打包,这种方式只适合判断少量文件,最好是单个文件是否存在。
2、将apk中的资源文件单独拷贝出来放到一个单独的文件夹下,比如/data/data/apkPackage/,当然拷贝也是很耗时的,通常这一步放到程序启动的时候处理,如同加载资源一样,这样做以后,可以直接判断这个文件夹里面是不是存在这个文件,对于同一个页面判断多个资源,是相当快的。
接下来讲一下第二种方式的具体作法:
在CCFileUtils类中添加两个方法:
1 bool CCFileUtils::isFileExist(const char* pFileName)
2 {
3 if( !pFileName ) return false;
4 //strFilePathName is :/data/data/ + package name
5 std::string filePath = m_obPreloadDirectory + pFileName;
6
7 FILE *fp = fopen(filePath.c_str(),"r");
8 if(fp)
9 {
10 fclose(fp);
11 return true;
12 }
13 return false;
14 }
15
16 void CCFileUtils::copyData(const char* pFileName)
17 {
18 std::string strPath = fullPathFromRelativePath(pFileName);
19 unsigned long len = 0;
20 unsigned char *data = NULL;
21
22 data = getFileData(strPath.c_str(),"r",&len);
23 if(data==NULL)
24 return;
25 std::string destPath = m_obPreloadDirectory;
26 destPath += pFileName;
27 FILE *fp = fopen(destPath.c_str(),"w+");
28 fwrite(data,sizeof(char),len,fp);
29 fclose(fp);
30 delete []data;
31 data = NULL;
32 }isFileExist可以用来判断文件是否存在,copyData就是将apk中的资源文件拷贝到另外一个目录,当然你可以选取你需要的资源进行拷贝,因为拷贝也是一项耗时的操作。这两个方法里面都有一个私有成员m_obPreloadDirectory,这其实就是你想把文件拷贝到哪里的目录,很简单,给它一个set方法,外界设置一下这个目录就可以了,一般情况都设为/data/data/packagename。
在拷贝资源前先通过isFileExist判断一下文件是否存在,存在就不需要再拷贝了,因为刚才说过拷贝很耗时。
接下来就可以通过isFileExist方法来判断文件是否存在了。(在不断的尝试中总结经验,以帮助后面的人更快的上路)


喜欢
顶
难过
囧
围观
无聊



