Two methods for obtaining bitmap from resources

Source: Internet
Author: User

Method (1): Obtain the resource first, then obtain the drawable through the resource ID, or obtain the data stream of the resource file through the resource ID. drawable is an abstract class, in bitmapdrawable, we can see the specific operations of Bitmap. After carefully looking at the bitmapdrawable constructor, we will find that it corresponds to the openrawresource () interface in resource, you can use the following method to obtain the bitmap:

Resources r = This. getcontext (). getresources ();
Inputstream is = R. openrawresource (R. drawable. my_background_image );
Bitmapdrawable BMP draw = new bitmapdrawable (is );
Bitmap BMP = BMP draw. getbitmap ();

Method (2): After obtaining the data stream of the resource file through the resource function: inputstream openrawresource (int id), you can also obtain bitmap in two ways, as shown below:
Use bitmapdrawable
(A drawable that wraps a bitmap and can be tiled, stretched, or aligned .)
Use bitmapdrawable (inputstream is) to construct a bitmapdrawable. Use getbitmap () of the bitmapdrawable class to obtain the bitmap;
Bitmapdrawable also provides operations such as display bitmap.
Use bitmapfactory
(Creates bitmap objects from various sources, including files, streams, and byte-arrays .)
Bitmapfactory class decodestream (inputstream is) is used to decode bitmap resources and obtain bitmap
All functions of bitmapfactory are static. This helper class can obtain bitmap through resource ID, path, file, and data stream.
The above methods can be freely selected during programming. In the android SDK, the supported image formats are as follows: PNG (preferred), JPG (acceptable), GIF (discouraged ), although the BMP format is not clearly stated, it is clearly stated in the android SDK support media format.

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.