I’m trying to load a text file from res/raw. I’ve looked at several code snippets and tried implementing a few ways but none seem to work for me. The code I currently am trying to get to work is this
TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.hello);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
But it suffers from te same problem as all the others do.
a) (TextView)findViewById(R.id.hellotxt); says it depreciated and Eclipses recommends Migrating code.
b) getResources() isn’t recognized and just suggests I add a method called getResources().
Initially I wanted to use assets folder but got the same error as b) but with getAssets().
This is a seperate class file I’m implementing this is called public class PassGen{} with one method at the moment called public String returnPass(){}
The functions getAssets and getResources should be called from a Context.
If you call it from within an Activity class it doesn’t need a prefix, but otherwise you’d need to pass the context to the class that needs the functions and call e.g. context.getAssets().