Cocos2dx file opening problem on android platform, cocos2dxandroid
We have a project that is based on cocos2dx + lua. protobuf is used in the network. When initializing protobuf, we need to read local files and use lua's io. open Reading files in windows, ios is okay, android app is apk file, because apk is compressed file, so io. open is powerless because the entire app is compressed into a file. You can call the getFileData () function in cocos2dx to read the file content. getFileData checks different platforms and calls different interfaces to open and read files, there is still no problem on ios and windows. You can still open the file when calling android to read the file. I tried many methods and couldn't solve the problem. I asked another colleague from the company to help me check the problem and solve the problem after modifying the path.
string relativePath = "scripts/test.pb"; // this is okstring relativePath = "./scripts/test.pb"; // this can not open the file"
At that time, we didn't know why this solution could be solved. At that time, we thought it was possible that linux didn't support this path. After going home, we went to the linux experiment, even if I added a few "./", it didn't affect opening the directory. View the underlying cocos2d-x, found that the android part is called in the ndk AAssetManager_open method, can only see the header file can not see the source code. Then I searched for information about this function on google, and I didn't get any help.
StackoverflowAsk a question. Some experts answered the question and referenced the source code of this part on github. After reading the source code, they wanted to understand that they did not open the file through this path, but went to a file manager to find it, my file path contains ". /", compared with the original path, it is naturally not found. At this point, this question is eliminated. This event is inspired by the following:
1. The benefits of open-source are that you only need to check the source code when you encounter problems. You can only view the document if you encounter problems with the source code. If the document is not clearly written, I think it is best not to use such a library. If you encounter problems, you will be crying.
2. Sometimes, when solving the problem, do not be too strong. technicians think that the problem cannot be solved, but sometimes it does happen: "I don't know the true colors of the mountains, but I am here ", you may find yourself out when you look for an outsider.
3. In the process of learning computer science, we will accumulate more than 1.1 pieces of data to lay a solid foundation. We will not want to seek fast and new ideas. We will also look at our experiences and foundations when encountering problems.
4. No matter what the book or celebrities say is not necessarily correct, it is correct only when something is tested by a computer.
Refer:
1,
Source code for this part on github
Problems with cocos2dx android Development
If you do not know the specific situation, I can import them normally.
Read and Write Cocos2d-x on android
Permission issues, it seems that reading and writing is not possible, but you can copy the files to be read and written to the data directory for reading and writing.