Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 727
Hong Avatar asked Mar 24 '12 20:03

Hong


1 Answers

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

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

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.

like image 73
yorkw Avatar answered Nov 04 '22 17:11

yorkw