如何从Android NDK获取资产中的文件

我正在尝试从本机端访问Assets文件夹中的图像文件。现在,我可以成功搜索资产文件夹及其子目录,以查找要查找的特定文件:


AAssetDir* assetDir = AAssetManager_openDir(asset_manager, "images");

const char* filename;

while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL)

{

   __android_log_print(ANDROID_LOG_DEBUG, "Debug", filename);

}


AAssetDir_close(assetDir);

我也在使用OSG。我使用以下代码将图像设置为着色器的纹理:


texture->setImage(osgDB::readImageFile(fileNameWithPath));

但是该文件名也必须包含绝对路径,因为它不在同一目录中。但是我发现我无法获得资产目录的绝对路径,因为它与APK一起压缩。


由于我无法访问绝对路径,我还可以使用其他什么方式来获取图像?另外要注意的是,我确实希望将文件保留在Asset文件夹中,以用作SD卡。


holdtom
浏览 867回答 2
2回答

FFIVE

您可以使用AAssetManager_open&读取资产的图像AAsset_read,但是由于资产位于apk中,因此无法获得文件路径名称-该文件路径也被压缩。您可以将数据保存到文件中,然后再从该文件中读取,或者,如果OSG允许,则可以直接处理从资产文件中获取的块。从这里: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);

炎炎设计

值得一提的另一件事是,android对可随时读取的资产大小设置了1Mb的限制。如果文件大于该文件,则必须将其拆分为多个块。这是我的工作解决方案,它将文件大块加载到chars的向量中:AAssetManager * mgr = app->activity->assetManager;&nbsp;AAssetDir* assetDir = AAssetManager_openDir(mgr, "");const char* filename;std::vector<char> buffer;while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL)&nbsp;{&nbsp; &nbsp; //search for desired file&nbsp; &nbsp; if(!strcmp(filename, /* searched file name */))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; AAsset *asset = AAssetManager_open(mgr, filename, AASSET_MODE_STREAMING);&nbsp; &nbsp; &nbsp; &nbsp; //holds size of searched file&nbsp; &nbsp; &nbsp; &nbsp; off64_t length = AAsset_getLength64(asset);&nbsp; &nbsp; &nbsp; &nbsp; //keeps track of remaining bytes to read&nbsp; &nbsp; &nbsp; &nbsp; off64_t remaining = AAsset_getRemainingLength64(asset);&nbsp; &nbsp; &nbsp; &nbsp; size_t Mb = 1000 *1024; // 1Mb is maximum chunk size for compressed assets&nbsp; &nbsp; &nbsp; &nbsp; size_t currChunk;&nbsp; &nbsp; &nbsp; &nbsp; buffer.reserve(length);&nbsp; &nbsp; &nbsp; &nbsp; //while we have still some data to read&nbsp; &nbsp; &nbsp; &nbsp; while (remaining != 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set proper size for our next chunk&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(remaining >= Mb)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currChunk = Mb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currChunk = remaining;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char chunk[currChunk];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //read data chunk&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(AAsset_read(asset, chunk, currChunk) > 0) // returns less than 0 on error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //and append it to our vector&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer.insert(buffer.end(),chunk, chunk + currChunk);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remaining = AAsset_getRemainingLength64(asset);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; AAsset_close(asset);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android