jsw
jsw

Reputation: 728

Get Resource From Name in External Class

I have a string that identifies a resource that i want to grab.

I know about the following:

public int getIdentifier(String name, String defType, String defPackage)

However i need to use it in an external class, and it won't let me.

For example i have a string called "thefile" and i want to access R.raw.thefile

Any ideas? I'm stuck

Cheers

Upvotes: 2

Views: 681

Answers (1)

Squonk
Squonk

Reputation: 48871

You could pass a Context to your external class. Example...

public Myclass {

    public void doSomething (Context context) {
        int resId = context.getResources().getIdentifier(...);

}

Then in your Activity...

MyClass myClass = new MyClass();
myClass.doSomething(this);

Upvotes: 5

Related Questions