2

I have spent a couple hours reading through the various hits in stackoverflow that result from my title query.

Using Android Studio.

I have a txt file in my src\main\assets project directory that I want to open and read.

All the examples I find use AssetManager() or getAssets() it seems.

I can't get usage of either to work.

InputStreamReader isr = new InputStreamReader(AssetManager.AssetInputStream("xxx.csv"));

Produces error: AssetManager() is not public in AssetManager; cannot be accessed from outside package

What do I need to do to get access to a file in my /assets folder, in order to open and read it?

Edit the problem with marking this as duplicate is that I had read that other post on this same subject, but the users original question (the same I have) is never answered!!! The answer goes about showing the user how to read a file. But the problem was (same as mine): method isn't recognized. In my case (see comment below on answer) I get error: cannot find symbol method getAssets().

Can anyone help me with this? All the examples show the sample code of

AssetManager am = getAssets();

But something is missing.

JJJones_3860
  • 1,382
  • 2
  • 15
  • 35

1 Answers1

4

What you might want to try is use:

AssetManager am = getAssets();
InputStream is;
is = am.open(pathToFile.txt);

Then use a byte[] buffer (get size using is.available(); ) and is.read(buffer) Don't forget to is.close() after read.

If you're not in an Activity you have to use context.getAssets() in order to get the AssetManager.

Hope that helps you.

  • This is the problem. I know how to open and read a file, I just don't seem to be able to use the AssetManager. When I put in the following line, "AssetManager am = getAssets();" the compilation error is: "error: cannot find symbol method getAssets()". I must be missing something important. – JJJones_3860 Apr 09 '16 at 17:28
  • Solved! I missed the part that if not in Activity class, then must use context to getAssets(). – JJJones_3860 Apr 09 '16 at 17:41
  • Nice, glad you solved it. I've edited my answer for the next people who needs it. – Primaël Quémerais Apr 09 '16 at 18:53