2

在我的 android 应用程序中,我需要拍照。第一张照片很好,上传到服务器,因此可以通过电子邮件发送给用户。这工作正常。然而,当我想上传第二张图片时,它会显示内存不足异常。我不知道为什么,但不知何故。

我的 logcat 输出可以在这个 pastebin 链接中找到:http: //pastebin.com/uVduy3d9

我处理图像的代码如下:

首先检查手机是否有摄像头:

Camera cam = Camera.open();
    if (cam != null) {
        if (savedInstanceState != null
                && savedInstanceState.getBoolean("Layout")) {
            setContentView(R.layout.registration_edit);
            initializeAccountDetails((User) savedInstanceState
                    .getSerializable(EXTRA_MESSAGE));
            inAccountDetails = true;
        } else {
            setContentView(R.layout.step_4);
            ((Button) findViewById(R.id.snap)).setOnClickListener(this);
            ((Button) findViewById(R.id.rotate)).setOnClickListener(this);
            cam.stopPreview();
            cam.release();
            cam = null;
        }
    } else {
        if (savedInstanceState != null
                && savedInstanceState.getBoolean("Layout")) {
            setContentView(R.layout.registration_edit);
            initializeAccountDetails((User) savedInstanceState
                    .getSerializable(EXTRA_MESSAGE));
            inAccountDetails = true;
        } else {
            setContentView(R.layout.step_4b);
        }
    }

当单击按钮 Snap 时,会触发以下 onClick 事件:

@Override
public void onClick(View v) {
    if (v.getId() == R.id.snap) {
        File directory = new File(Environment.getExternalStorageDirectory()
                + "/BandenAnalyse/Images/");
        if (directory.exists()) {
            Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
            File f = new File(Environment.getExternalStorageDirectory(),
                    "/BandenAnalyse/Images/IMG_" + _timeStamp + ".jpg");
            _mUri = Uri.fromFile(f);
            i.putExtra(MediaStore.EXTRA_OUTPUT, _mUri);
            startActivityForResult(i, TAKE_PICTURE);
        } else {
            directory.mkdir();
            this.onClick(v);
        }
    } else {
        if (_mPhoto != null) {
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            _mPhoto = Bitmap.createBitmap(_mPhoto, 0, 0,
                    _mPhoto.getWidth(), _mPhoto.getHeight(), matrix, true);
            ((ImageView) findViewById(R.id.photo_holder))
                    .setImageBitmap(_mPhoto);
_mPhoto.recycle();
        }
    }
}

拍摄照片时,将触发 result 方法:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            getContentResolver().notifyChange(_mUri, null);
            ContentResolver cr = getContentResolver();
            try {
                _mPhoto = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, _mUri);

                Display display = getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int width = size.x;
                int scale = _mPhoto.getWidth() / width;
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inSampleSize = 8;
                Debug.out(PATH_TO_PHOTO);
                Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o);

                _mPhoto = Bitmap.createScaledBitmap(
                                            temp,
                                            _mPhoto.getWidth() / scale, _mPhoto.getHeight()
                                                    / scale, false);

                temp.recycle();
                ((ImageView) findViewById(R.id.photo_holder))
                        .setImageBitmap(_mPhoto);
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

错误应该出现在最后一种方法中,因为当我处于相机模式并想要返回我的应用程序时,就会发生错误。如何修复此错误?我错过了什么?

编辑:

在函数中添加代码:OnActivityResult。如解决方案之一所述,创建了一个临时对象。太糟糕了,这无助于解决错误。

错误 Out of memory Exception 发生在以下行:

_mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, _mUri);
4

1 回答 1

3

您不应该创建位图并在没有保留参考的情况下使用它,因为您必须释放它以进行良好的内存管理。

你做什么 :

_mPhoto = Bitmap.createScaledBitmap(
                        BitmapFactory.decodeFile(PATH_TO_PHOTO, o),
                        _mPhoto.getWidth() / scale, _mPhoto.getHeight()
                                / scale, false);

不好 !;)

更喜欢 :

Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o);

_mPhoto = Bitmap.createScaledBitmap(
                            temp,
                            _mPhoto.getWidth() / scale, _mPhoto.getHeight()
                                    / scale, false);

temp.recycle(); //this call is the key ;) 

请牢记阅读您的代码:“创建的每个位图都必须进行回收,否则它会在某些时候因 OOM 错误而崩溃”。

希望有帮助!

您应该阅读有关android Bitmaps 和内存管理的更多信息以获得完整的理解;)

于 2013-11-04T10:13:16.960 回答