I want to create a utility class that loads images contained in the assets/ file structure. It would be tiresome to require all calls to it, or any other utility class calling it, to pass along the Context all the way from the UI.
Would it be possible to just pass it down once, and store it (as long as the utility class is static or a singleton)? Or would that have devastating consequences memory wise?
Also, so no one asks, I’m using the assets/ folder instead of res/ because I want sub-folders, and to access images by name dynamically, which I can’t do using resources to my knowledge. For example, I want my sprite object to load all images in the specific sprite’s folder, rather than hard coding R.drawable.sprite1front, R.drawable.sprite1leftside, etc.
*edit – Copying the AssetManager would also be fine, if that can be done.
You can use a singleton or a static class for your utility class, but you should initialize it with a reference to an Application object, not an Activity. (Keeping a reference to an Activity is a good way to leak memory.) The best way might be to subclass Application and use that as your utility class. You’ll have to declare your Application class in the manifest.