Troubleshooting for OOM exceptions caused by loading large images on Android

Source: Internet
Author: User

Dalvik imposes limits on the maximum memory of Android applications, while parsing images is resource-consuming. For example, parsing a 2048*1536 bitmap requires 12 Mb of memory, which usually results in oom.


Solution: reduce the quality of the image to be loaded Based on the device resolution. For example, if the device resolution is 480*320, you only need to set the image to be loaded (for example, 2048*1536) you can compress it to 480*320. The following describes how to compress the android SDK:

/*** Calculate the image compression ratio based on the actual image size and the size to be displayed. * @ Param options * @ Param reqwidth: display width * @ Param reqheight: display height * @ return compression ratio */Public static int calculateinsamplesize (bitmapfactory. options options, int reqwidth, int reqheight) {// The actual width and height of final int Height = options. outheight; Final int width = options. outwidth; int insamplesize = 1; if (height> reqheight | width> reqwidth) {final int heightratio = math. round (float) Height /(Float) reqheight); Final int widthratio = math. Round (float) width/(float) reqwidth); insamplesize = heightratio <widthratio? Heightratio: widthratio;} return insamplesize;}/*** according to the display size, get the compressed image * @ Param res * @ Param resid * @ Param reqwidth display width * @ Param reqheight display height * @ return */public static bitmap decodesampledbitmapfromresource (Resources res, int resid, int reqwidth, int reqheight) {final bitmapfactory. options = new bitmapfactory. options (); // do not compress to obtain the actual width and height options. injustdecodebounds = true; bitmapfactory. decoderesource (Res, resid, options); // calculate the compression ratio options. insamplesize = calculateinsamplesize (options, reqwidth, reqheight); // sets the options for compression. injustdecodebounds = false; return bitmapfactory. decoderesource (Res, resid, options );}
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.