我可以在 Android NDK 中使用 ifstream 访问资产吗? [英] Can I use ifstream in Android NDK to access Assets?

查看:47
本文介绍了我可以在 Android NDK 中使用 ifstream 访问资产吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,但我很难在网上找到有关此的任何信息.

My question is pretty simple, but I am having a hard time finding any info about this online.

是否可以使用 ifstream 从使用 Android NDK 的资产和/或资源中打开文件?

Is it possible to use ifstream to open a file from assets and/or resources using Android NDK?

例如,将 test.txt 文件放在/assets 中并尝试以下操作不起作用:

For example, placing a test.txt file in /assets and trying the following does not work:

    char pLine[256];
    std::ifstream fin("/assets/test.txt");
    if(!fin.fail())
    {
        LOGD( "test.txt opened" );
        while( !fin.eof() )
        {
            fin.getline( pLine, 256 );
            LOGD(pLine);
        }
    }
    else
    {
        LOGD( "test.txt FAILED TO OPEN!" );
    }
    fin.close();

也没有任何变量:

    std::ifstream fin("assets/test.txt");

    std::ifstream fin("test.txt");

等等...,也不要把它放在/res 中.

Etc..., nor placing it in /res instead.

那么,是否可以使用普通的 ifstream 操作符来访问资产和/或资源文件?

So, is it possible to use normal ifstream operators to access assets and or resource files?

推荐答案

不,你不能.资产存储在 apk(一个 zip 文件)中.ifstream 无法在 zip 文件中读取.

No, you cannot. Assets are stored within the apk, a zip file. ifstream cannot read within the zip file.

要访问这些文件,您需要在 java 中访问它们并将它们保存在其他地方,或者提取 apk 的内容以获取资产.

To access these files you either need to access them in java and save them elsewhere or extract the contents of the apk to get to the assets.

这里有一个做前者的例子.

Here is an example of doing the former.

http://www.itwizard.ro/android-phone-installing-assets-how-to-60.html

以下是执行后者的示例.

Here is an example of doing the latter.

http://www.anddev.org/ndk_opengl_-_loading_resources_and_assets_from_native_t11978.html/a>

http://www.anddev.org/ndk_opengl_-_loading_resources_and_assets_from_native_code-t11978.html

这篇关于我可以在 Android NDK 中使用 ifstream 访问资产吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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