0

I am trying to load assets from the assets folder into my Android application using Android Studio and Java

The code is simple, but it does not work

AssetManager assetManager = MainActivity.getAssets();
InputStream ims = assetManager.open("helloworld.txt");   

The error is Non-static method 'getAssets()' cannot be referenced from a static context

There is no word static anywhere in MainActivity class, was it statically instantiated? HGow can I get around this? Where do I need to read the assets? And how can I properly get assets if this is incorrect? Additionally what imports do I need for AssetManager, I cannot find this

Mi Po
  • 1,123
  • 2
  • 12
  • 21
  • Regarding your error, see ["Non-static method cannot be referenced from a static context" error](https://stackoverflow.com/q/4922145/6395627). – Slaw Mar 04 '23 at 23:50

1 Answers1

0

Simply use getAssets() method when you are calling inside the Activity class.

If you calling this method in non-Activity class then you need to call this method from Context which is passed from Activity class. So below is the line by you can access the method.

ContextInstance.getAssets();

ContextInstance may be passed as this of Activity class.

krishnan
  • 132
  • 5