1

Every answer on this question is incomplete, and I get a lot of errors when trying to use the code.

Example: read file from assets

AssetManager am = MainActivity.getAssets();
InputStream is = am.open("book.txt");

I get errors:

  1. Non-static method 'getAssets()' cannot be referenced from a static context
  2. Cannot resolve Symbol "AssetManager", which Import do I need for this?
  3. context.getAssets(); context is undefined, what context do I need? MainActivity? what function do I use to get the Context of MainActivity? What imports do I need to make this work?
  4. Do I need to add something to Gradle files? And what?
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
Mich
  • 3,188
  • 4
  • 37
  • 85
  • Maybe helpful: [Android How to read an asset from outside of main activity](https://stackoverflow.com/q/4126387/295004) ,[What is the difference between Activity and Context?](https://stackoverflow.com/q/6518206/295004) Generally avoid calling any Android APIs outside of a Application/Activity/Service class or method without taking into consideration the lifecycle. – Morrison Chang Mar 04 '23 at 22:59

0 Answers0