Home Android - writing/saving files from native code only
Reply: 1

Android - writing/saving files from native code only

celavek Published in 2012-07-02 13:16:27Z

I'm trying to build an Android app which makes use of the NativeActivity facility of the NDK. I'm having the following structure:

  • a bunch of native shared libraries installed in /system/vendor/<company>; I'm working with a custom built Android image so there's no problem having the libraries there with proper permissions and everything
  • a couple of applications using the NativeActivity that depend in turn on the libraries mentioned above

The libraries installed in the /system/vendor and my applications use a couple of configuration files. There's no problem reading them using the standard C API fopen/fclose. But those libraries and my application also need to store some files as the result of their operation, like configuration, some run-time parameters, calibration data, log files etc. With the storing of the files there is a slight issue as I'm not allowed to write into /system/vendor/... (as the file system under "/system/..." is mounted read-only and I do not want to hack on that).

So what would be the best way to create and store those files and where would be the best "conforming with Android" storage area ?

I've been reading a couple of threads in the android-ndk Google group and here on SO that mention either the internal application private storage or the external SD card, but as I do not have extended experience with Android I'm not sure what would be the proper approach. If the approach involves some specific Android API a small code example in C++ would be very helpful; I've seen a couple of examples involving Java and JNI (e.g. in this SO question) but I would like to stay away from that right now. Also there seems to be a problem with using from C++ the native activity's internalDataPath/externalDataPath pair (a bug that makes them be always NULL).

ItamarG3 Reply to 2017-06-26 14:48:44Z

For relatively small files(application config files, parameter files, log files etc.) is best to use the internal application private storage, that is /data/data/<package>/files. The external storage if it exists at all (being it SD card or not) should be used for large files that do not need frequent access or updates.

For the external data storage the native application has to "request" the correct permissions in the application's AndroidManifest.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> 

For the internal application private storage fopen/fclose(or C++ stream equivalents if available) API could be used. Following example illustrates using the Android NDK AssetManager to retrieve and read a configuration file. The file must be placed into the assets directory inside the native application’s project folder so that the NDK build could pack them inside the APK. The internalDataPath/externalDataPath bug I was mentioning in the question was fixed for the NDK r8 version.

void android_main(struct android_app* state)
    // Make sure glue isn't stripped 

    ANativeActivity* nativeActivity = state->activity;                              
    const char* internalPath = nativeActivity->internalDataPath;
    std::string dataPath(internalPath);                               
    // internalDataPath points directly to the files/ directory                                  
    std::string configFile = dataPath + "/app_config.xml";

    // sometimes if this is the first time we run the app 
    // then we need to create the internal storage "files" directory
    struct stat sb;
    int32_t res = stat(dataPath.c_str(), &sb);
    if (0 == res && sb.st_mode & S_IFDIR)
        LOGD("'files/' dir already in app's internal data storage.");
    else if (ENOENT == errno)
        res = mkdir(dataPath.c_str(), 0770);

    if (0 == res)
        // test to see if the config file is already present
        res = stat(configFile.c_str(), &sb);
        if (0 == res && sb.st_mode & S_IFREG)
            LOGI("Application config file already present");
            LOGI("Application config file does not exist. Creating it ...");
            // read our application config file from the assets inside the apk
            // save the config file contents in the application's internal storage
            LOGD("Reading config file using the asset manager.\n");

            AAssetManager* assetManager = nativeActivity->assetManager;
            AAsset* configFileAsset = AAssetManager_open(assetManager, "app_config.xml", AASSET_MODE_BUFFER);
            const void* configData = AAsset_getBuffer(configFileAsset);
            const off_t configLen = AAsset_getLength(configFileAsset);
            FILE* appConfigFile = std::fopen(configFile.c_str(), "w+");
            if (NULL == appConfigFile)
                LOGE("Could not create app configuration file.\n");
                LOGI("App config file created successfully. Writing config data ...\n");
                res = std::fwrite(configData, sizeof(char), configLen, appConfigFile);
                if (configLen != res)
                    LOGE("Error generating app configuration file.\n");
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.384292 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO