从外部类中的名称获取资源

jsw*_*jsw 2 android

我有一个字符串,用于标识我想要抓取的资源.

我知道以下内容:

public int getIdentifier(String name, String defType, String defPackage)
Run Code Online (Sandbox Code Playgroud)

但是我需要在外部类中使用它,它不会让我.

例如,我有一个名为"thefile"的字符串,我想访问R.raw.thefile

有任何想法吗?我被卡住了

干杯

Squ*_*onk 5

您可以将Context传递给外部类.例...

public Myclass {

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

}
Run Code Online (Sandbox Code Playgroud)

然后在你的活动......

MyClass myClass = new MyClass();
myClass.doSomething(this);
Run Code Online (Sandbox Code Playgroud)