在Android库项目中可以读取没有上下文引用的原始文本文件 [英] Is it possible to read a raw text file without Context reference in an Android library project
问题描述
我可以把一个文本文件放在一个库项目的文件夹中,但是看起来似乎需要一个上下文引用。任何人都可以这么说吗?
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?
推荐答案
查看我的答案这里了解如何从POJO读取文件。
Check out my answer here to see how to read file from POJO.
通常,res文件夹应该被ADT插件自动添加到项目构建路径中。假设你有一个test.txt存储在res / raw文件夹下,读取它不需要android.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屋!