Two ways for Android to obtain bitmap

Source: Internet
Author: User

Method 1 JavaCode

    1. // Obtain the resources object
    2. Resources r =This. Getcontext (). getresources ();
    3. // Read resources using data streams
    4. Inputstream is = R. openrawresource (R. drawable. my_background_image );
    5. Bitmapdrawable BMP draw =NewBitmapdrawable (is );
    6. 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

    1. inputstream is = getresources (). openrawresource (R. drawable. Icon);
    2. bitmap mbitmap = bitmapfactory. decodestream (is);
    3. paint mpaint = New paint ();
    4. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.