2

I am working on adding achievement to an app. I have a global achievements controller that is in charge of updating and setting achievements to complete. I want to assign a bitmap to each achievement, that will be displayed when the achievement has been completed.

I am trying to do the follow in the AchievementController:

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.someImage);
Achievement.setBitmap(bitmap);

But the method getresources will not work, because there is no context.

I cannot pass the context as a parameter to this class, because it is static.

So, I need some way to set bitmaps in a static class, without using a context

user2782067
  • 382
  • 4
  • 19
  • 1
    Possible duplicate of [Static way to get 'Context' on Android?](https://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android) – Shailendra Madda Nov 27 '17 at 06:03
  • 1
    those answers all use weird hacks to get a context, and they admittedly don't guarantee that they'll even be called in time. I'd rather use some way to get a bitmap without having to use a context at all, which is what my question is asking for. – user2782067 Nov 27 '17 at 06:10
  • If you want to use system resources you need to use like `Resources.getSystem().getString(android.R.string.cancel)` or else you should pass context to it. If you want local resources see [here](https://stackoverflow.com/a/4391811/2462531) – Shailendra Madda Nov 27 '17 at 06:25
  • 1
    so, there is no good way to simply assign a bitmap to a class object, without using some weird workaround to pass context? – user2782067 Nov 27 '17 at 21:23
  • Yes, as the name suggests, it's the context of current state of the application/object. – Shailendra Madda Nov 28 '17 at 04:49

0 Answers0