Search code examples
androidcastingbitmapassignment-operator

how to assign an R.drawable folder image to a Bitmap object


How do I load an image of jpg or png type in the R.drawable folder of android into a Bitmap object?

Bitmap bmp = (Bitmap) putThisImageIntoBitmap(R.drawable.myimage);

Solution

  • First you have to use the BitmapFactory class, then decode the resource file with the drawable's id and context's resources as parameters.

    Sample:

    putThisImageIntoBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.myimage));
    
    • if your class extends an "Activity" class, you could just use "this" or "getApplicationContext()" method.
    • the second parameter is the id of the drawable you want to get