0

I need to get the context to be able to get a resource. Like this:

getApplicationContext().getResources().openRawResource( R.raw.texture );

I've seen the getApplicationContext() in the android documentation but when I try to use it in the above code it doesn't work - it doesn't exist.

I can send the context through functions to get it to where it's needed and it works. However, I find it cumbersome to send a variable through many functions that doesn't need or use it. Then I would rather just try to get it in the function that do. But the getApplicationContext(), as in the android documentation, doesn't work - http://developer.android.com/reference/android/content/Context.html

So how do I get the context so I can read resources? Or are my only option to send it through all my functions?

Espen
  • 3,607
  • 11
  • 48
  • 75

2 Answers2

1

getApplicationContext() is a method of a Context. You have to have a context to get the resources. That's just how it works.

Just makes sure you're not storing a reference to your context anywhere or you could cause a memory leak.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
0

In your activity you can use getResources() method immediately. For example

getResources().getDrawable(R.drawable.logo);

If you want to get some resource in some other class not in activity, you should pass context link in other class from your activity. For example

Util.convertLogo(this)

or

Util.convertLogo(getApplicationContext())
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78