安卓下不能读取文件

问题:在Classes/Config/DataString.cpp里面有读文件的操作,在IOS,Mac,window平台均可正常读取stringl.xml文件,编译到安卓之后就读取文件失败,返回结果为3,详细信息看截图

和这个问题类似 http://www.cocoachina.com/bbs/read.php?tid=296582&page=1#1289977
如果fopen 这样来打开文件的话,是不可以操作assets目录下的文件的。可以尝试把要操作的文件放入sd卡,或者用用FileUtils里的getDataFromFile 接口操作

已经解决,我的解决办法

//获取文件绝对路径
//输入参数:
// relativePath:文件的相对路径
//返回值:
// 成功:文件绝对路径
// 失败:返回""
std::string getFullFilePath(std::string relativePath)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//首先查询在可写目录是否已经存在该文件
std::string writePath = FileUtils::getInstance()->getWritablePath() + relativePath;
FILE *pFp = fopen(writePath.c_str(), “r”);
if (pFp)
{
CCLog(“CAppData::getFullFilePath()----found object\n”);
fclose(pFp);
return writePath;
}

//可写文件不存在该文件,则拷贝到可写目录
auto data = FileUtils::getInstance()->getDataFromFile(relativePath);
if(data.isNull())
{
    CCLog("CAppData::getFullFilePath()----don't found file\n");
    return "";
}
else
{
    size_t len = data.getSize();//文件长度
    
    //获取程序可写路径
    std::string writePath = FileUtils::getInstance()->getWritablePath();
    writePath+= relativePath;
    
    //将文件复制
    pFp=fopen(writePath.c_str(),"wb");
    
    if(pFp == nullptr)
    {
        CCLog("CAppData::getFullFilePath()----open write file Error\n");
        return "";
    }
    
    fwrite(data.getBytes(),sizeof(char),len,pFp);
    
    //马上保存
    fflush(pFp);
    fclose(pFp);
    
    CCLog("CAppData::getFFullFilePath()----create <%s> file success\n", writePath.c_str());
    //返回文件路径
    return writePath;
}

#else
return FileUtils::getInstance()->fullPathForFilename(relativePath);
#endif
}