如何让文件在资产的Andr​​oid NDK [英] How To Get File In Assets From Android NDK

查看:282
本文介绍了如何让文件在资产的Andr​​oid 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。我设置的图像作为纹理着色器我用下面的code:

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

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

但是,文件名必须包含的绝对路径,以及因为它不位于在同一目录中。不过,我发现,我不能让这些资产目录的绝对路径,是它为com pressed随着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你不能得到它一个文件路径名 - 这也是COM pressed那里。您可以将数据保存到一个文件,后来从文件中读取,也可以直接处理您从资源文件得到了一块,如果OSG允许。

You can read the image from asset with AAssetManager_open & AAsset_read, but since asset lies in apk you can't get a filepath 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.

从<一个href="http://en.wikibooks.org/wiki/OpenGL_Programming/Android_GLUT_Wrapper#Accessing_assets">here:

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);

这篇关于如何让文件在资产的Andr​​oid NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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