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?
3 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.
-
Thanks for the tip. I have just tried. Eclipse does not like putting a file under res, so I let the file stay in res\raw. I get null when trying to access the file by this.getClass().getClassLoader().getResourceAsStream("text.txt") or this.getClass().getClassLoader().getResourceAsStream("raw\text.txt") – Hong Mar 24 '12 at 21:55
-
1@Hong, I've tried on my Mac with SDK r16 and now I can confirm both "res/raw/test.txt" and "raw/test.txt" works. whereas "test.txt" throws NPE. Note that you need slash (/), not back slash (\\). – yorkw Mar 24 '12 at 22:21
-
Yes!!! It works! It works in a static constructor too so that such reading does not have to be carried out every time the method is called. Thanks a lot. – Hong Mar 24 '12 at 22:31
-
1Thanks a lot for testing it. I am also using r16. InputStream in = Myclass.class.getClassLoader().getResourceAsStream("res/raw/txt.txt") works, but InputStream in = Myclass.class.getClassLoader().getResourceAsStream("raw/txt.txt") returns null. – Hong Apr 01 '12 at 15:27
-
@Hong, Weird, I've also tried on my Windows box (SDK r16), which also works for both patterns. Are you sure you use **raw/txt.txt**, not with a leading slash **/raw/txt.txt**? As **/raw/txt.txt** throws NPE. – yorkw Apr 01 '12 at 22:28
-
1Yes. To make sure, I have just tried again with or without the leading slash. This is in a library. Something like MyApp referencing MyClass that is in a library project which has the file txt.txt. – Hong Apr 02 '12 at 03:04
-
Oh, this makes sense. I didn't test it from a Library Project, the Library Project classpath is probably altered when get merged/built into Main project. Good point to know. – yorkw Apr 02 '12 at 03:39
-
This answer worked very well for over 8 years. A recent Android Studio and its associated components' updates seem to have broken it. https://stackoverflow.com/questions/67423363/getclassloader-getresourceasstreamfile-path-returns-null-after-android-studi – Hong May 07 '21 at 12:21
In order to access a resource you need a context. See here the definition of Context.class from the developer.android site
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
So, through a context you can access a resource file. You can create another class and pass the context from an activity to it. Create a method that reads a specified resource file.
For example:
public class ReadRawFile {
//Private Variable
private Context mContext;
/**
*
* Default Constructor
*
* @param context activity's context
*/
public ReadRawFile(Context context){
this.mContext = context;
}
/**
*
* @param str input stream used from readRawResource function
* @param x integer used for reading input stream
* @param bo output stream
*/
private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){
//not hitting end
while(x!=-1){
//write to output buffer
bo.write(x);
try {
//read next
x = str.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @return output file to string
*/
public String readRawResource(){
//declare variables
InputStream rawUniversities = mContext.getResources().openRawResource(R.raw.universities);
ByteArrayOutputStream bt = new ByteArrayOutputStream();
int universityInteger;
try{
//read/write
universityInteger = rawUniversities.read();
writeBuffer(rawUniversities, universityInteger, bt);
}catch(IOException e){
e.printStackTrace();
}
//return string format of file
return bt.toString();
}
}

- 453
- 4
- 22
-
Thanks for the response. As my understanding of your answer, there is no way to read a raw text without a Context. – Hong Mar 24 '12 at 20:55
It is possible to get raw file without context in 2021
val Int.rawString: String
get(){
var res= ""
val `is`: InputStream = Resources.getSystem().openRawResource(this)
val baos = ByteArrayOutputStream()
val b = ByteArray(1)
try {
while (`is`.read(b) !== -1) {
baos.write(b)
}
res = baos.toString()
`is`.close()
baos.close()
} catch (e: IOException) {
e.printStackTrace()
}
return res
}

- 168
- 1
- 6
- 7