Is it possible to read a file content from apk?

Is it possible to read a file content from apk?


I use 3rd party libs as source code, which allow me to create sprites from *.svg files. This method work fine with my build for Win32 platform, but not with my Androin builds.
The main problem on Android occurs when I have to open, read and parse *.svg file.
All my *.svg files are placed in assets/res/ folder. May be I should place my *.svg files in some other location?!
What is the proper way to open and read a file within apk using Android NDK? Is it possible at all?
What options do I have? Why Android build system is so complicated and messy? :slight_smile:

As you can see below fopen is used to open “assets/res/pieceOne.svg” file with “rb” mode. After debug’s “step over” fopen fails and file handle is NULL.

Have you tried making the file path as res/pieceOne.svg instead of assets/res/pieceOne.svg? It might be looking at the assets folder already, so might be trying assets/assets/res/pieceOne.svg. Not sure if you are using the FileUtils from cocos2d-x etc. Worth a shot to look around the cocos2d-x implementations of grabbing from assets folder.

Also this might be of interest if you are willing to use JNI and Java classes in the Android project.

1 Like


Looking at cocos2d-x implementation shows me the way to solve the problem :slight_smile:

Here is how I did it:

const std::string& fullPath = FileUtils::getInstance()->fullPathForFilename(path);

Data buf = FileUtils::getInstance()->getDataFromFile(fullPath);

std::string svgContentStr;
svgContentStr.assign(reinterpret_cast<const char*>(buf.getBytes()), buf.getSize());

_dom = svgdom::load(svgContentStr);


1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.