是否有可能读取没有上下文参考的原始文本文件中的一个Android库项目 [英] Is it possible to read a raw text file without Context reference in an Android library project

查看:113
本文介绍了是否有可能读取没有上下文参考的原始文本文件中的一个Android库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以把文件夹水库\库项目的原始文本文件,但阅读它似乎需要一个Context引用。任何人都可以揭示出这个一些轻?

I could put a text file in folder res\raw of a library project, but reading it seems to require a Context reference. Could anyone shed some light on this?

推荐答案

看看我的答案<一href="http://stackoverflow.com/questions/9503114/android-test-project-reading-assets-file-to-test-plain-java-object/9506449#9506449">here看看如何从POJO读取文件。

Check out my answer here to see how to read file from POJO.

通常情况下,资源文件夹应自动添加到由ADT插件项目构建路径。假设你有下RES /原始文件夹中存储的test.txt,读它,而不an​​droid.content.Context:

Generally, the res folder should be automatically added into project build path by ADT plugin. suppose you have a test.txt stored under res/raw folder, to read it without android.content.Context:

String file = "raw/test.txt"; // res/raw/test.txt also work.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);

我有一个老版本的SDK之前这样做,就应该用最新的SDK正常工作。试试吧,看看这会有所帮助。

I did this before with an old SDK version, it should work with latest SDK as well. Give it a try and see if this helps.

这篇关于是否有可能读取没有上下文参考的原始文本文件中的一个Android库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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