机器人:位图大小超过VM预算 [英] android : bitmap size exceeds VM budget

查看:118
本文介绍了机器人:位图大小超过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);    }

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


解决方案

使用


  

Options.inSampleSize


  //德code图像尺寸
BitmapFactory.Options选项=新BitmapFactory.Options();
//options.inJustDe$c$cBounds = TRUE;
options.inSampleSize = 8;
BitmapFactory.de codeStream(新ByteArrayInputStream的(缓冲),空,期权);

I am using android Camera. Also using auto focusing feature. After capturing image more then 10 times getting the following exception:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

my source code is following:

    PictureCallback jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {

                try {
                    // Bitmap bmp = BitmapFactory.decodeStream(new
                    // ByteArrayInputStream(data));
                    Utility.gc();
                    Bitmap bmp = decodeFile(data);
                    Utility.gc();
                    // BitmapFactory.decodeByteArray(data, 0,
                    // data.length, o);
                    // Bitmap bmpCompressed = rotateBitmap(bmp, 90, 320, 430);

                    Bitmap bmpCompressed = Bitmap.createScaledBitmap(bmp, 430, 320,
                            true);
                    bmp.recycle();
                    Utility.gc();
                    writeBmp(bmpCompressed);
                    bmpCompressed.recycle();
                    Utility.gc();

                } catch (Exception e) {
                    Log.e(Constants.TAG, e.getMessage(), e);

                } finally {
                    isImageCapture = true;
                }

            }

        };
private Bitmap decodeFile(byte[] buffer) {

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

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new ByteArrayInputStream(buffer),
                null, o2);

    }

how can i get rid from the above mention exception?

解决方案

Use

Options.inSampleSize

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆