简体   繁体   中英

Copy a file from assets to internal memory of android using c++ in cocos2dx

I am working on cocos2dx version 3.0-alpha0.

I want to copy a txt that is present in assets folder to the internal memory of android device.

So far i have

`   string originalPath = FileUtils::getInstance()->fullPathForFilename("example.txt");
    string dbPath = FileUtils::getInstance()->getWritablePath();
    dbPath.append("example.txt");
    FILE* source = fopen(originalPath.c_str(), "rb");

    if (source!=NULL)
    {
        log("source example open");
    }else{
        log("source example not open");
    }
    FILE* dest = fopen(dbPath.c_str(), "wb");
    if (dest!=NULL)
    {
        log("dest example open");
    }else{
        log("dest example not open");
    }
    unsigned long size2 = 0;
    unsigned char* smth = FileUtils::getInstance()->getFileData(originalPath.c_str(), "r", &size2);
    log("Size: %lu\n\n",size2);
    log("size:%zu",fread(buf, size2, BUFSIZ, source));

    // clean and more secure
    // feof(FILE* stream) returns non-zero if the end of file indicator for stream is set

    while ((size = fread(buf, 1, BUFSIZ, source))) {
        fwrite(buf, 1, size, dest);
    }

    fclose(source);
    log("establishConnection9");
    fclose(dest);
    log("establishConnection10");

`

In case of ios it works fine but in case of android . Getting file from assets is not that much easy.

In android ndk we have asset_manager.h

But i dont know how to work with one. I found few helpful links but dont know how to initialize things in those.

http://en.wikibooks.org/wiki/OpenGL_Programming/Android_GLUT_Wrapper#Accessing_assets

Here is another similar question

How To Get File In Assets From Android NDK

but i dont know how to get mgr

AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
const char* filename = (const char*)NULL;
while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
    AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_STREAMING);
    char buf[BUFSIZ];
    int nb_read = 0;
    FILE* out = fopen(filename, "w");
    while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0)
        fwrite(buf, nb_read, 1, out);
    fclose(out);
    AAsset_close(asset);
}
AAssetDir_close(assetDir);

All application resources on Android platform stored into APK files (that is simple Zip archive actually). We need to extract file from APK, then save it to internal storage. Cocos2d-x already have all features you need.

//First - get asset file data:
auto data =  FileUtils::getInstance()->getDataFromFile(filePath);

//second - save it:
string dbPath = FileUtils::getInstance()->getWritablePath() + path;

FILE* dest = fopen(dbPath.c_str(), "wb");
fwrite(data.getBytes(), 1, data.getSize(), dest);
fclose(dest);

Cocos2d-x FileUtils::getInstance()->getDataFromFile(filePath) checks if filePath is APK asset path, then unzip this file (if it in APK) or read it from storage device (for other files).

Note: this solution is NOT threadsafe

As @Selvin said,
you need to pass AssetManager object from java to native layer via jni. Here are some example.

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
AAssetManager   *mgr;

void Java_pe_berabue_ex_GLView_nativeCreated(JNIEnv * env, jclass cls, jobject assetManager)
{
    char *buf;
    int fileSize;

    mgr = AAssetManager_fromJava(GetEnv(), assetManager);

    AAsset* asset = AAssetManager_open(mgr, "tmp.txt", AASSET_MODE_UNKNOWN);    

    if ( asset == NULL )
        return;

    fileSize = AAsset_getLength(asset);

    if ( fileSize == 0 )
        return;

    buf = (char *)malloc(sizeof(char)*fileSize);
    AAsset_read(asset, buf, fileSize);

    AAsset_close(asset);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM