位图URI保持相同的Android Out Of Memory Exception

时间:2013-11-04 11:35:19

标签: android exception bitmap uri out-of-memory

我有一个问题,我正在拍摄图像并将其上传到服务器。这是第一次一切都很棒。 但是第二次我正在拍照,我在这一行得到了一个Out Of Memory Exception:

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

如果我正在调试_mUri,那么第一次和第二次是相同的。我认为这不应该......

我收到此错误:http://pastebin.com/uVduy3d9

这是我用来拍照的3种方法:

首先检查手机是否有相机:

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);
        }
    }

单击按钮时,会触发以下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);
        }
    }
}

拍摄照片时,会触发结果方法:

@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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我想我必须补充一下:

android:largeHeap="true"

到我的清单文件。这让我可以多次这样做。