How to set a bitmap from resource

Beginner picture Beginner · Feb 10, 2011 · Viewed 274.2k times · Source

This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.

bm = BitmapFactory.decodeResource(null, R.id.image);

Is this correct?

Answer

xandy picture xandy · Feb 10, 2011

Assuming you are calling this in an Activity class

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

The first parameter, Resources, is required. It is normally obtainable in any Context (and subclasses like Activity).