Android: OOM error caused by Image File Scaling

Source: Internet
Author: User

Prepared by: Xu jianxiang (
Date: 2010/12/06


To transfer a file or set an avatar, we usually check the size of the original image for scaling.


Common Java version scaling Image Code:


Public Bitmap getZoomImage (Bitmap src, int desW, int desH) <br/>{< br/> Bitmap desImg = null; <br/> int srcW = src. getWidth (); // original image width <br/> int srcH = src. getHeight (); // The height of the original image <br/> int [] srcBuf = new int [srcW * srcH]; // cache the pixel information of the original image </p> <p> src. getPixels (srcBuf, 0, srcW, 0, 0, srcW, srcH ); </p> <p> // calculate the interpolation table <br/> int [] tabY = new int [desH]; <br/> int [] tabX = new int [desW]; </p> <p> int sb = 0; <br/> int db = 0; <br/> int tems = 0; <br/> int temd = 0; <br/> int distance = srcH> desH? SrcH: desH; <br/> for (int I = 0; I <= distance; I ++) <br/> {/* vertical direction */<br/> tabY [db] = sb; <br/> tems + = srcH; <br/> temd + = desH; <br/> if (tems> distance) <br/>{< br/> tems-= distance; <br/> sb ++; <br/>}< br/> if (temd> distance) <br/>{< br/> temd-= distance; <br/> db ++; <br/>}</p> <p> sb = 0; <br/> db = 0; <br/> tems = 0; <br/> temd = 0; <br/> distance = srcW> desW? SrcW: desW; </p> <p> for (int I = 0; I <= distance; I ++) <br/> {/* horizontal direction */<br/> tabX [db] = (short) sb; <br/> tems ++ = srcW; <br/> temd + = desW; <br/> if (tems> distance) <br/> {<br/> tems-= distance; <br/> sb ++; <br/>}< br/> if (temd> distance) <br/>{< br/> temd-= distance; <br/> db ++; <br/>}</p> <p> // generate a scaled-down image pixel. <br/> int [] desBuf = new int [desW * desH ]; <br/> int dx = 0; <br/> int dy = 0; <br/> int sy = 0; <br/> int oldy =-1; </p> <p> for (int I = 0; I <desH; I ++) <br/>{< br/> if (oldy = tabY [I]) <br/>{< br/> System. arraycopy (desBuf, dy-desW, desBuf, dy, desW); <br/>}< br/> else <br/>{< br/> dx = 0; <br/> for (int j = 0; j <desW; j ++) <br/>{< br/> desBuf [dy + dx] = srcBuf [sy + tabX [j]; <br/> dx ++; <br/>}< br/> sy + = (tabY [I]-oldy) * srcW; <br/>}< br/> oldy = tabY [I]; <br/> dy + = desW; <br/>}< br/> // generate an image <br/> desImg = Bitmap. createBitmap (desBuf, desW, desH, Bitmap. config. ARGB_8888); </p> <p> return desImg; <br/>} 


Common Android image scaling code:


Contentresolver Cr = This. getcontentresolver (); <br/> try <br/>{< br/> inputstream in = CR. openinputstream (URI); <br/> Bitmap bitmap = bitmapfactory. decodestream (in); <br/> try <br/>{< br/> in. close (); <br/>}< br/> catch (ioexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> If (null = Bitmap) <br/>{< br/> toast. maketext (this, "head is not set successful, decode bitmap failure", 2000 ); <br/>}< br/> // size of the original image <br/> int BMP width = bitmap. getwidth (); <br/> int bmpheight = bitmap. getheight (); </P> <p> // scale the image size <br/> float scalewidth = (float) 40/BMP width; <br/> float scaleheight = (float) 40/bmpheight; <br/> matrix = new matrix (); <br/> matrix. postscale (scalewidth, scaleheight); </P> <p> // generates a scaled bitmap object <br/> bitmap resizebitmap = bitmap. createbitmap (<br/> bitmap, 0, 0, BMP width, bmpheight, matrix, false); <br/> bitmap. recycle (); <br/> // bitmap to byte [] <br/> byte [] photodata = bitmap2bytes (resizebitmap ); </P> <p> // save file <br/> string filename = "/sdcard/test.jpg"; <br/> fileutil. writetofile (filename, photodata); </P> <p> // save photo check sum to DB <br/> datacenter. getinstance (). modifyimmuser (); <br/> // refresh imageview <br/>}< br/> catch (filenotfoundexception exp) <br/>{< br/> exp. printstacktrace (); <br/>} 


If the image size is very large, an OOM exception will be thrown when BitmapFactory. decodeStream is executed.


Let's take a look at how the system uses MMS. After the multimedia attachment is added to SMS, MMS is processed. When the source image of the attached file exceeds 300 kb, the system also performs scaling. For details, refer: com. android. mms. ui /. uriImage:


Package COM. android. MMS. ui; <br/> public class uriimage <br/>{< br/> private int mwidth; <br/> private int mheight; <br/> ...... <br/> // <br/> private void decodeboundsinfo () <br/>{< br/> inputstream input = NULL; <br/> try <br/> {<br/> input = mcontext. getcontentresolver (). openinputstream (muri); <br/> bitmapfactory. options opt = new bitmapfactory. options (); <br/> OPT. injustdecodebounds = true ;/ /Only stroke, not read data <br/> bitmapfactory. decodestream (input, null, OPT); <br/> mwidth = OPT. outwidth; <br/> mheight = OPT. outheight; <br/>}< br/> catch (filenotfoundexception e) <br/>{< br/> // ignore <br/> log. E (TAG, "ioexception caught while opening stream", e); <br/>}< br/> finally <br/>{< br/> If (null! = Input) {<br/> try {<br/> input. close (); <br/>}catch (ioexception e) {<br/> // ignore <br/> log. E (TAG, "ioexception caught while closing stream", e ); <br/>}< br/> private byte [] getresizedimagedata (INT widthlimit, int heightlimit) <br/>{< br/> int outwidth = mwidth; <br/> int outheight = mheight; <br/> int S = 1; <br/> while (outwidth/S> widthlimit) | (outheight /S> heightlimit) <br/>{< br/> S * = 2; <br/>}< br/> // set the option first <br/> bitmapfactory. options = new bitmapfactory. options (); <br/> // returning a smaller image to save memory. <br/> options. insamplesize = s; <br/> inputstream input = NULL; <br/> try <br/> {<br/> input = mcontext. getcontentresolver (). openinputstream (muri); <br/> bitmap B = bitmapfactory. decodestream (input, null, options); // note Usage of options <br/> If (B = NULL) {<br/> return NULL; <br/>}< br/> bytearrayoutputstream OS = new bytearrayoutputstream (); <br/> B. compress (compressformat. JPEG, messageutils. image_compression_quality, OS); <br/> return OS. tobytearray (); <br/>} catch (filenotfoundexception e) {<br/> log. E (TAG, E. getmessage (), e); <br/> return NULL; <br/>}finally {<br/> If (input! = NULL) {<br/> try {<br/> input. close (); <br/>}catch (ioexception e) {<br/> log. E (TAG, E. getmessage (), e); <br/>}< br/> ...... <br/>} 


It can be seen that the MMS application method is: first set the scaling option, and then read the scaled image data to the memory, avoiding memory-caused OOM.


Modified code:


ContentResolver cr = this. getContentResolver (); <br/> try <br/>{< br/> InputStream in = cr. openInputStream (uri); <br/> BitmapFactory. options options = new BitmapFactory. options (); <br/> options. inJustDecodeBounds = true; <br/> BitmapFactory. decodeStream (in, null, options); <br/> try <br/> {<br/> in. close (); <br/>}< br/> catch (IOException e) <br/>{< br/> e. printStackTrace (); <br/>}< br/> int mWidth = options. outWidth; <br/> int mHeight = options. outHeight; </p> <p> int sWidth = 40; <br/> int sHeight = 40; </p> <p> int s = 1; <br/> while (mWidth/s> sWidth * 2) | (mHeight/s> sHeight * 2 )) <br/>{< br/> s * = 2; <br/>}< br/> options = new BitmapFactory. options (); <br/> options. inSampleSize = s; <br/> in = cr. openInputStream (uri); <br/> Bitmap bitmap = BitmapFactory. decodeStream (in, null, options); <br/> try <br/> {<br/> in. close (); <br/>}< br/> catch (IOException e) <br/>{< br/> e. printStackTrace (); <br/>}< br/> if (null = bitmap) <br/>{< br/> Toast. makeText (this, "Head is not set successful, Decode bitmap failure", 2000); <br/> return; <br/>}< br/> // size of the original image <br/> int BMP width = bitmap. getWidth (); <br/> int bmpHeight = bitmap. getHeight (); </p> <p> // resize the image <br/> float scaleWidth = (float) sWidth/BMP width; <br/> float scaleHeight = (float) sHeight/bmpHeight; <br/> Matrix matrix = new Matrix (); <br/> matrix. postScale (scaleWidth, scaleHeight); </p> <p> // generates a scaled Bitmap object <br/> Bitmap resizeBitmap = Bitmap. createBitmap (<br/> bitmap, 0, 0, BMP width, bmpHeight, matrix, false); <br/> bitmap. recycle (); <br/> // Bitmap resizeBitmap = bitmap; <br/> // Bitmap to byte [] <br/> byte [] photoData = bitmap2Bytes (resizeBitmap ); </p> <p> // save file <br/> String fileName = "/sdcard/test.jpg"; <br/> FileUtil. writeToFile (fileName, photoData ); 


Private byte [] bitmap2Bytes (Bitmap bm) <br/>{< br/> ByteArrayOutputStream baos = new ByteArrayOutputStream (); <br/> bm. compress (Bitmap. compressFormat. JPEG, 100, baos); <br/> return baos. toByteArray (); <br/>} 



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: 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.