本文介绍了机器人:位图大小超过VM预算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android相机。还使用自动对焦功能。拍摄图像超过10次获得以下异常后:结果
java.lang.OutOfMemoryError:引起的位图的大小超过VM预算

我的源$ C ​​$ c为以下内容:

  PictureCallback jpegCallback =新PictureCallback(){
            公共无效onPictureTaken(字节[]数据,相机摄像头){                尝试{
                    // BMP位图= BitmapFactory.de codeStream(新
                    // ByteArrayInputStream的(数据));
                    Utility.gc();
                    BMP位图=去codeFILE(数据);
                    Utility.gc();
                    // BitmapFactory.de codeByteArray的(数据,0,
                    // data.length,O);
                    //位图bmpCom pressed = rotateBitmap(BMP,90,320,430);                    位图bmpCom pressed = Bitmap.createScaledBitmap(BMP,430,320,
                            真正);
                    bmp.recycle();
                    Utility.gc();
                    writeBmp(bmpCom pressed);
                    bmpCom pressed.recycle();
                    Utility.gc();                }赶上(例外五){
                    Log.e(Constants.TAG,e.getMessage(),E);                } {最后
                    isImageCapture = TRUE;
                }            }        };
私人位图德codeFILE(字节[]缓冲区){        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(新ByteArrayInputStream的(缓冲),空,O);        //我们希望新的大小缩放到
        最终诠释REQUIRED_SIZE = 70;        //找到正确的比例值。它应该是2的幂。
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }        //德code。与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeStream(新ByteArrayInputStream进行(缓冲区)
                空,O2);    }

我怎样才能从上面提到的例外摆脱?


解决方案

使用

// Decode image size
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inJustDecodeBounds = true;
options.inSampleSize = 8; 
BitmapFactory.decodeStream(new ByteArrayInputStream(buffer), null, options);

这篇关于机器人:位图大小超过VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:29