0

I've tried, searched a lot of things here, but non of those seem to work. (The file has 5000 rows) I keep getting error massages. For example with AssetManager: Cannot resolve method 'getAssets()' -> and in the code getAssets is red.

private static InputStream getReadTextFromAssets() throws IOException {
    AssetManager assetManager = getAssets();
    InputStream file = assetManager.open("tryToRead.txt");
    return file;
}
Cubbi
  • 9
  • 2
  • 1
    If it is in non activity class then you need to use specific context or use `getApplicationContext()`. – Piyush Mar 14 '17 at 08:19

2 Answers2

1

I had the same problem for getting files from assets folder. So instead I created a raw folder inside res directory, then copied my files insideraw folder. Now getting the files from raw folder was very easy. See below link

How to read file from res/raw by name

You can also see below link if you want to stick with asset folder.

How to reference a File in raw folder in Android

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0

Method getAssets() is accessable via instance of Resources class,

pass that as argument to your method:

private static InputStream getReadTextFromAssets(Resources myRes) throws IOException {}

and use it to call getAssets()

myRes.getAssets()

and for using your method, pass Resources when invoking method

InputStram is = getReadTextFromAssets(getResources());

Again, getResources() needs Context class [Activity] or an instance of Context obtained from an Activity.

Yazan
  • 6,074
  • 1
  • 19
  • 33