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 );}