Method 1 JavaCode
- // Obtain the resources object
- Resources r =This. Getcontext (). getresources ();
- // Read resources using data streams
- Inputstream is = R. openrawresource (R. drawable. my_background_image );
- Bitmapdrawable BMP draw =NewBitmapdrawable (is );
- Bitmap BMP = BMP draw. getbitmap ();
// Obtain the resources object Resources r = This. getcontext (). getresources (); // read the resource inputstream is = R. openrawresource (R. drawable. my_background_image); bitmapdrawable BMP draw = new bitmapdrawable (is); bitmap BMP = BMP draw. getbitmap ();
The second method is to use the bitmapfactory tool class. All functions of bitmapfactory are static. This helper class can obtain bitmap through resource ID, path, file, and data stream. You can open the API to see if it is a static method. There is a decodestream (inputstream is) in this class)
This method can decode a new bitmap from an inputstream. This is the inputstream for obtaining resources.
Code: Java code
- inputstream is = getresources (). openrawresource (R. drawable. Icon);
- bitmap mbitmap = bitmapfactory. decodestream (is);
- paint mpaint = New paint ();
- canvas. drawbitmap (mbitmap, 40 ,
40 , mpaint);
Inputstream is = getresources (). openrawresource (R. drawable. icon); bitmap mbitmap = bitmapfactory. decodestream (is); paint mpaint = new paint (); canvas. drawbitmap (mbitmap, 40, 40, mpaint );
Obviously, the second method is much simpler.