简体   繁体   English

使用cocos2dx中的C ++将文件从资产复制到android的内部存储器

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

I am working on cocos2dx version 3.0-alpha0. 我正在研究cocos2dx版本3.0-alpha0。

I want to copy a txt that is present in assets folder to the internal memory of android device. 我想将资产文件夹中存在的txt复制到android设备的内部存储器中。

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 . 在ios的情况下工作正常,但在android的情况下。 Getting file from assets is not that much easy. 从资产获取文件并不是那么容易。

In android ndk we have asset_manager.h 在android ndk中,我们拥有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 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 如何从Android NDK获取资产中的文件

but i dont know how to get mgr 但我不知道如何获得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). Android平台上的所有应用程序资源都存储到APK文件中(实际上是简单的Zip存档)。 We need to extract file from APK, then save it to internal storage. 我们需要从APK中提取文件,然后将其保存到内部存储中。 Cocos2d-x already have all features you need. Cocos2d-x已经具有您需要的所有功能。

//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). Cocos2d-x FileUtils::getInstance()->getDataFromFile(filePath)检查filePath是否为APK资产路径,然后解压缩此文件(如果在APK中)或从存储设备读取(对于其他文件)。

Note: this solution is NOT threadsafe 注意:此解决方案不是线程安全的

As @Selvin said, 正如@Selvin所说,
you need to pass AssetManager object from java to native layer via jni. 您需要通过jni将AssetManager对象从Java传递到本机层。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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