【问题标题】:java.lang.OutOfMemoryError in android while saving picture taken from cameraandroid中的java.lang.OutOfMemoryError,同时保存从相机拍摄的照片
【发布时间】:2011-12-07 16:50:13
【问题描述】:

我有一个应用程序,我需要在从相机拍摄图像后将它们保存到 SD 卡中。

代码如下:

camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                        myPictureCallback_JPG);

PictureCallback myPictureCallback_JPG = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {

            Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
                    arg0.length);

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(UploadedFilename);
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            final Bitmap result = Bitmap.createScaledBitmap(bitmapPicture, 640,
                    480, false);

这一行的代码炸弹:

位图bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

上面写着:

异常类 java.lang.OutOfMemoryError 源方法 BitmapFactory.nativeDecodeByteArray()。

请帮忙

【问题讨论】:

标签: android bitmap android-camera out-of-memory


【解决方案1】:

如果您只想将图片存储到 SD 卡中,则无需创建位图。假设您想要获得 >= 640px 宽的图像:

final int DESIRED_WIDTH = 640;

// Set inJustDecodeBounds to get the current size of the image; does not
// return a Bitmap
final BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
Log.d(TAG, "Bitmap is " + sizeOptions.outWidth + "x"
            + sizeOptions.outHeight);

// Now use the size to determine the ratio you want to shrink it
final float widthSampling = sizeOptions.outWidth / DESIRED_WIDTH;
sizeOptions.inJustDecodeBounds = false;
// Note this drops the fractional portion, making it smaller
sizeOptions.inSampleSize = (int) widthSampling;
Log.d(TAG, "Sample size = " + sizeOptions.inSampleSize);

// Scale by the smallest amount so that image is at least the desired
// size in each direction
final Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length,
        sizeOptions);

BitmapFactory.Options中还有很多其他有趣的设置

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 2023-03-21
    • 2010-11-12
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多