I want to access a text file located under assets from a class that would do some processing on that file and is invoked in my Activity. The code I am using is:
AssetManager assetManager = getAssets();
InputStream instream = assetManager.open("file.txt");
this works fine if used in an Activity class but how to make it work in a plain java class?
Edited
JamesBlack – because if i pass the inputstream then the following code won’t work
for (int i = 0; i < gestureNumbers.size(); i++) {
BufferedReader bf = new BufferedReader(new InputStreamReader(instream));
// possible error here
while ((line = bf.readLine()) != null) { ... }
}
Even if the gestureNumbers.size() > 1 then the while loop is executed only once and then it equals null once it read the file once.
When the following code works but I can’t open the file in plain java class, it works in activity class and I don’t want to have too much code in one class.
for (int i = 0; i < gestureNumbers.size(); i++) {
InputStream instream = assetManager.open("BSL_Word_POS.txt");
BufferedReader bf = new BufferedReader(new InputStreamReader(instream));
// possible error here
while ((line = bf.readLine()) != null) { ... }
}
You have to pass the context to that java class to access the asset folder.