Android reads local resources in the most memory-saving way

Source: Internet
Author: User
Method 1:/*** get the local image and specify the height and width */public static bitmap getnativeimage (string ImagePath) {bitmapfactory. options = new bitmapfactory. options (); options. injustdecodebounds = true; // obtain the width and height of the image. Bitmap mybitmap = bitmapfactory. decodefile (ImagePath, options); // at this time, the returned mybitmap is null // calculate the zoom ratio int be = (INT) (options. outheight/(float) 200); int ys = options. outheight % 200; // calculate the remainder float Fe = YS/(float) 200; If (Fe> = 0.5) be = be + 1; if (be <= 0) be = 1; options. insamplesize = be; // re-read the image. Note that options should be added this time. injustdecodebounds is set to falseoptions. injustdecodebounds = false; mybitmap = bitmapfactory. decodefile (ImagePath, options); Return mybitmap;} Method 2: /*** read images of local resources in the most memory-saving mode * @ Param context * @ Param resid * @ return */public static bitmap readbitmap (context, int resid) {bitmapfactory. options opt = new bitmapfactory. options (); OPT. inpreferredconfig = bitmap. config. rgb_565; OPT. inpurgeable = true; OPT. ininputtransferable = true; // obtain the resource image inputstream is = context. getresources (). openrawresource (resid); Return bitmapfactory. decodestream (is, null, OPT);} method 3: /*** use the most memory-saving method to read images from local resources or images in sdcard * @ Param ImagePath * path of the image in sdcard * @ return */public static bitmap getsdcardimg (string ImagePath) {bitmapfactory. options opt = new bitmapfactory. options (); OPT. inpreferredconfig = bitmap. config. rgb_565; OPT. inpurgeable = true; OPT. ininputretriable = true; // obtain the resource image return bitmapfactory. decodefile (ImagePath, OPT );}

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.