0

将文本文件实现到 android 应用程序的最佳方法是什么?

我正在创建一个需要使用文本文档作为资源的应用程序 - 我如何包含它,以便当用户安装应用程序时文档与它一起并且应用程序可以访问它?

4

3 回答 3

5

assets/(通过访问AssetManager)或res/raw/(通过访问Resources)可能是打包“文本文档”的解决方案。

于 2013-03-12T16:33:17.857 回答
1

把它放在你的资产文件夹中,然后通过你的资产文件夹访问文本文件

于 2013-03-12T16:33:49.093 回答
1

我建议将您的文本文件放在两个地方,文件assets/夹或res/raw/文件夹。下面是在每个位置访问您的文件的示例。对于下面的示例,假设您的文件名为example.txt.

资产文件夹:

AssetManager assets = getAssets();
InputStream in = assets.open("example.txt");
/* Use the input stream to read in your file here. */

原始目录:

Resources res = getResources();
InputStream in = res.openRawResource(R.raw.example);
/* Use the input stream to read in your file here. */

请注意,这两个都假设您正在从Context诸如Activityor之类的类中打开资源Service

于 2013-03-12T17:13:59.090 回答