本文介绍了java.lang.OutOfMemoryError在Android的同时节约照片从相机拍摄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序中,我需要从相机拍摄后,他们救我的图像转换成SD卡。

I have an app in which I need to save my images into sdcard after taking them from camera.

下面是code:

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

在此行中的code炸弹:

The code bombs on this line:

位图bitmapPicture = BitmapFactory.de codeByteArray(为arg0,0,                        arg0.length);

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

它说,它的:

Exception类java.lang.OutOfMemoryError源法BitmapFactory.nativeDe codeByteArray()。

Exception class java.lang.OutOfMemoryErrorSource method BitmapFactory.nativeDecodeByteArray().

请帮

推荐答案

如果你只是想将图片保存到SD卡中,就没有必要创建位图。比方说,你想获得的图像也就是> = 640px宽:

If you just want to store the picture to the SD card, there's no need to create a Bitmap. Let's say you want to get an image which is >= 640px wide:

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

There are lots of other interesting settings in BitmapFactory.Options

这篇关于java.lang.OutOfMemoryError在Android的同时节约照片从相机拍摄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 09:05