0

I have been trying to use getResources in a non-activity class. I found some advice on how to do so here. To use one of the suggested ways, by Lilzilala, (there are multiple, but mostly suggest the same thing), I have created a special class, used this to specify the resources as "res", and then instantiated this class using "new" in a line which invokes "getResources".

However, I'm getting a "cannot resolve method getResources" error on "getResources". I'm a bit of a noob, but don't know why this is happening. From what I can tell, this error happens when there simply isn't a resource with that name available. Which makes me think maybe Resources doesn't contain getResources() by default?

class executeTrimmer<Resdefine> {


    public class ResDefine {
        private Resources res;

        public ResDefine(Resources res)
        {
            this.res = res;
        }}


 Bitmap img1 = BitmapFactory.decodeResource(new ResDefine(getResources()),
        R.drawable.bmpname);
}

EDIT - following suggestions that I add context, I have tried this:

class executeTrimmer<Resdefine> {


private static Context context;
public executeTrimmer(Context context){
    this.context = context;
}

    public class ResDefine {
        private Resources res;

        public ResDefine(Resources res)
        {
            this.res = res;
        }}


 Bitmap img1 = BitmapFactory.decodeResource(new ResDefine(executeTrimmer.context.getResources),
        R.drawable.bmpname);

But this still brings up error "cannot resolve symbol getResources". I've tried multiple different ways to pass context to it, and consistently faced the same error.

Statsanalyst
  • 331
  • 2
  • 3
  • 16

1 Answers1

0

As you can see in the official documentation, "getResources" is Context's method, therefore you can't call it out from nowhere, neither statically. This method requires a context instance.

In your case you must at least pass a context to your class to be able to invoke it as next:

context.getResources()

I think you got confused from seen it being directly called inside Activities without a prefixed context, but as all Activities are actually a context, this is why there is no prefix.

To clarify. When called inside an activity, this:

getResources()

is the same as this:

this.getResources()

where the prefix "this." refers to the activity, which in turn is a context by itself.

On the other hand your code should be like next, without the ResDefine class. And notice that the decodeResource call is required to be inside a method and not at class level scope (this is not allowed in Java). And in fact you don't even need to use a context, so pass instead the Resources instance from the caller's class which is supposed to hold the context:

public class executeTrimmer {

    private final Resources res;
    
    public executeTrimmer(final Resources res) {
        this.res= res;
    }
    
    public void loadBitmap()
        Bitmap img1 = BitmapFactory.decodeResource(this.res, R.drawable.bmpname);
        ........
    }
}

And for the caller, next a very naive example, so may get an idea:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        new executeTrimmer(this.getResources()).loadBitmap();
    }
}
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127