网上有很多解决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().
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
[size=1em]37
[size=1em]38
[size=1em]39
[size=1em]40
[size=1em]41
[size=1em]42
| [size=1em][size=1em]public static int computeSampleSize(BitmapFactory.Options options,
[size=1em] int minSideLength, int maxNumOfPixels) {
[size=1em] int initialSize = computeInitialSampleSize(options, minSideLength,
[size=1em] maxNumOfPixels);
[size=1em]
[size=1em] int roundedSize;
[size=1em] if (initialSize <= 8) {
[size=1em] roundedSize = 1;
[size=1em] while (roundedSize < initialSize) {
[size=1em] roundedSize <<= 1;
[size=1em] }
[size=1em] } else {
[size=1em] roundedSize = (initialSize + 7) / 8 * 8;
[size=1em] }
[size=1em]
[size=1em] return roundedSize;
[size=1em]}
[size=1em]
[size=1em]private static int computeInitialSampleSize(BitmapFactory.Options options,
[size=1em] int minSideLength, int maxNumOfPixels) {
[size=1em] double w = options.outWidth;
[size=1em] double h = options.outHeight;
[size=1em]
[size=1em] int lowerBound = (maxNumOfPixels == -1) ? 1 :
[size=1em] (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
[size=1em] int upperBound = (minSideLength == -1) ? 128 :
[size=1em] (int) Math.min(Math.floor(w / minSideLength),
[size=1em] Math.floor(h / minSideLength));
[size=1em]
[size=1em] if (upperBound < lowerBound) {
[size=1em] return lowerBound;
[size=1em] }
[size=1em]
[size=1em] if ((maxNumOfPixels == -1) &&
[size=1em] (minSideLength == -1)) {
[size=1em] return 1;
[size=1em] } else if (minSideLength == -1) {
[size=1em] return lowerBound;
[size=1em] } else {
[size=1em] return upperBound;
[size=1em] }
[size=1em]}
|
以上只做为参考,我们只要用这函数即可,opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
要点:
1、用decodeFileDescriptor()来生成bimap比decodeFile()省内存
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
| [size=1em][size=1em]FileInputStream is = = new FileInputStream(path);
[size=1em]bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
|
替换
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
| [size=1em][size=1em]Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
[size=1em] imageView.setImageBitmap(bmp);
|
原因:
查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap decodeFile源码:
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
| [size=1em][size=1em]public static Bitmap decodeFile(String pathName, Options opts) {
[size=1em] Bitmap bm = null;
[size=1em] InputStream stream = null;
[size=1em] try {
[size=1em] stream = new FileInputStream(pathName);
[size=1em] bm = decodeStream(stream, null, opts);
[size=1em] } catch (Exception e) {
[size=1em] /* do nothing.
[size=1em] If the exception happened on open, bm will be null.
[size=1em] */
[size=1em] } finally {
[size=1em] if (stream != null) {
[size=1em] try {
[size=1em] stream.close();
[size=1em] } catch (IOException e) {
[size=1em] // do nothing here
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em] return bm;
[size=1em]}
|
decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap decodeFileDescriptor源码:
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
| [size=1em][size=1em] public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) {
[size=1em] if (nativeIsSeekable(fd)) {
[size=1em] Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
[size=1em] if (bm == null && opts != null && opts.inBitmap != null) {
[size=1em] throw new IllegalArgumentException("Problem decoding into existing bitmap");
[size=1em] }
[size=1em] return finishDecode(bm, outPadding, opts);
[size=1em] } else {
[size=1em] FileInputStream fis = new FileInputStream(fd);
[size=1em] try {
[size=1em] return decodeStream(fis, outPadding, opts);
[size=1em] } finally {
[size=1em] try {
[size=1em] fis.close();
[size=1em] } catch (Throwable t) {/* ignore */}
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em]private static native Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,Rect padding, Options opts);
|
2、当在android设备中载入较大图片资源时,可以创建一些临时空间,将载入的资源载入到临时空间中。
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
| [size=1em][size=1em]opts.inTempStorage = new byte[16 * 1024];
|
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
完整代码:
[size=1em][backcolor=rgb(108, 226, 108) !important][color=white !important][size=1em] ?
[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
[size=1em]37
[size=1em]38
[size=1em]39
[size=1em]40
[size=1em]41
[size=1em]42
[size=1em]43
[size=1em]44
[size=1em]45
[size=1em]46
[size=1em]47
[size=1em]48
[size=1em]49
[size=1em]50
[size=1em]51
[size=1em]52
| [size=1em][size=1em]public static OutputStream decodeBitmap(String path) {
[size=1em] BitmapFactory.Options opts = new BitmapFactory.Options();
[size=1em] opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
[size=1em] BitmapFactory.decodeFile(path, opts);
[size=1em] opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
[size=1em] opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
[size=1em] opts.inPurgeable = true;
[size=1em] opts.inInputShareable = true;
[size=1em] opts.inDither = false;
[size=1em] opts.inPurgeable = true;
[size=1em] opts.inTempStorage = new byte[16 * 1024];
[size=1em] FileInputStream is = null;
[size=1em] Bitmap bmp = null;
[size=1em] InputStream ins = null;
[size=1em] ByteArrayOutputStream baos = null;
[size=1em] try {
[size=1em] is = new FileInputStream(path);
[size=1em] bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts); double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600);
[size=1em] Bitmap bmp2 = Bitmap.createScaledBitmap(bmp,
[size=1em] (int) (opts.outWidth * scale),
[size=1em] (int) (opts.outHeight * scale), true);
[size=1em] bmp.recycle();
[size=1em] baos = new ByteArrayOutputStream();
[size=1em] bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
[size=1em] bmp2.recycle();
[size=1em] return baos;
[size=1em] } catch (FileNotFoundException e) {
[size=1em] e.printStackTrace();
[size=1em] } catch (IOException e) {
[size=1em] e.printStackTrace();
[size=1em] } finally {
[size=1em] try {
[size=1em] is.close();
[size=1em] ins.close();
[size=1em] baos.close();
[size=1em] } catch (IOException e) {
[size=1em] e.printStackTrace();
[size=1em] }
[size=1em] System.gc();
[size=1em] }
[size=1em] return baos;
[size=1em] }
[size=1em]private static double getScaling(int src, int des) {
[size=1em]/**
[size=1em] * 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比
[size=1em] */
[size=1em] double scale = Math.sqrt((double) des / (double) src);
[size=1em] return scale;
[size=1em]}
|
|