网上有很多解决android加载bitmap内存溢出的方法,搜了一圈做下整理总结。项目里需求是拍摄多图之后上传,部分手机会内存溢出。
常用一种解决方法:即将载入的图片缩小,这种方式以牺牲图片的质量为代价。在BitmapFactory中有一个内部类BitmapFactory.Options,其中当options.inSampleSize值>1时,根据文档:
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 -> decodes full size; 2 -> decodes 1/4th size; 4 -> decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.
options.inSampleSize是以2的指数的倒数被进行放缩
现在问题是怎么确定inSampleSize的值?每张图片的放缩大小的比例应该是不一样的!这样的话就要运行时动态确定。在BitmapFactory.Options中提供了另一个成员inJustDecodeBounds。
设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。Android提供了一种动态计算的方法,见computeSampleSize().
publicstaticintcomputeSampleSize(BitmapFactory.Options options,intminSideLength,intmaxNumOfPixels){intinitialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);introundedSize;if(initialSize <=8) { roundedSize =1;while(roundedSize < initialSize) { roundedSize <<=1; } }else{ roundedSize = (initialSize +7) /8*8; }returnroundedSize;}privatestaticintcomputeInitialSampleSize(BitmapFactory.Options options,intminSideLength,intmaxNumOfPixels){doublew = options.outWidth;doubleh = options.outHeight;intlowerBound = (maxNumOfPixels == -1) ?1: (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));intupperBound = (minSideLength == -1) ?128: (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));if(upperBound < lowerBound) {returnlowerBound; }if((maxNumOfPixels == -1) && (minSideLength == -1)) {return1; }elseif(minSideLength == -1){returnlowerBound; }else{returnupperBound; }}
以上只做为参考,我们只要用这函数即可,opts.inSampleSize = computeSampleSize(opts, -1,128*128);
要点:
1、用decodeFileDescriptor()来生成bimap比decodeFile()省内存
FileInputStreamis= =newFileInputStream(path);bmp = BitmapFactory.decodeFileDescriptor(is.getFD(),null, opts);
替换
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts); imageView.setImageBitmap(bmp);
原因:
查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap
decodeFile源码:
publicstaticBitmapdecodeFile(String pathName, Options opts){ Bitmap bm =null; InputStream stream =null;try{ stream =newFileInputStream(pathName); bm = decodeStream(stream,null, opts); }catch(Exception e) {/* do nothing.
If the exception happened on open, bm will be null.
*/}finally{if(stream !=null) {try{ stream.close(); }catch(IOException e) {// do nothing here} } }returnbm; }
decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap
decodeFileDescriptor源码:
publicstaticBitmapdecodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts){if(nativeIsSeekable(fd)) { Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts);if(bm ==null&& opts !=null&& opts.inBitmap !=null) {thrownewIllegalArgumentException("Problem decoding into existing bitmap"); }returnfinishDecode(bm, outPadding, opts); }else{ FileInputStream fis =newFileInputStream(fd);try{returndecodeStream(fis, outPadding, opts); }finally{try{ fis.close(); }catch(Throwable t) {/* ignore */} } } }privatestaticnativeBitmapnativeDecodeFileDescriptor(FileDescriptor fd,Rect padding, Options opts);
2、当在android设备中载入较大图片资源时,可以创建一些临时空间,将载入的资源载入到临时空间中。
opts.inTempStorage =newbyte[16*1024];
完整代码:
publicstaticOutputStream decodeBitmap(Stringpath) {BitmapFactory.Options opts =newBitmapFactory.Options();opts.inJustDecodeBounds =true;// 设置成了true,不占用内存,只获取bitmap宽高BitmapFactory.decodeFile(path, opts);opts.inSampleSize = computeSampleSize(opts, -1,1024*800);opts.inJustDecodeBounds =false;// 这里一定要将其设置回false,因为之前我们将其设置成了trueopts.inPurgeable =true;opts.inInputShareable =true;opts.inDither =false;opts.inPurgeable =true;opts.inTempStorage =newbyte[16*1024];FileInputStream is =null;Bitmap bmp =null;InputStream ins =null;ByteArrayOutputStream baos =null;try{is =newFileInputStream(path);bmp = BitmapFactory.decodeFileDescriptor(is.getFD(),null, opts);doublescale= getScaling(opts.outWidth * opts.outHeight,1024*600);Bitmap bmp2 = Bitmap.createScaledBitmap(bmp,(int) (opts.outWidth *scale),(int) (opts.outHeight *scale),true);bmp.recycle();baos =newByteArrayOutputStream();bmp2.compress(Bitmap.CompressFormat.JPEG,100, baos);bmp2.recycle();returnbaos;}catch(FileNotFoundException e) {e.printStackTrace();}catch(IOException e) {e.printStackTrace();}finally{try{is.close();ins.close();baos.close();}catch(IOException e) {e.printStackTrace();}System.gc();}returnbaos;}privatestaticdoublegetScaling(intsrc,intdes) {/**
* 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比
*/doublescale= Math.sqrt((double) des / (double) src);returnscale;}
http://my.oschina.net/jeffzhao/blog/80900