如何从 Android NDK 获取资产中的文件 [英] How To Get File In Assets From Android NDK

查看:39
本文介绍了如何从 Android NDK 获取资产中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to access an image file in the assets folder from the native side. Now I can successfully search through the assets folder and its subdirectories locating the particular file that I am looking for:

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.我使用以下代码将图像设置为着色器的纹理:

I am also using OSG. I set the image as a texture to my shader with the following code:

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

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

But that file name must include the absolute path as well since it is not located in the same directory. However I found out that I cannot get the absolute path for the assets directory, being that it is compressed along with the APK.

由于我无法访问绝对路径,我还可以使用什么其他方式来获取图像?另请注意,我确实希望将资产文件夹中的文件与 SD 卡保持一致.

Being that I can't access the absolute path, what other way could I use to get the image? Also to note, I do want to keep the files in the asset folder as appose to the sd card.

推荐答案

您可以使用 AAssetManager_open 从资产中读取图像 &AAsset_read,但是由于资产位于 apk 中,因此您无法获得它的文件路径名 - 它也在那里被压缩.您可以将数据保存到一个文件并稍后从该文件中读取,或者如果 OSG 允许,您可以直接处理您从资产文件中获得的块.

You can read the image from an asset with AAssetManager_open & AAsset_read, but since asset lies in apk you can't get a file path name for it - it is also compressed there. You can save the data to a file and read from that file later or you can directly process the chunk you got from your asset file if OSG allows.

来自此处:

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 NDK 获取资产中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆