Android - onPictureTaken OutOfMemory

摘要:Android - onPictureTaken OutOfMemory

有些手機,或許記憶體很小,發生了OutOfMemory 的問題。

 

經網路查,可能是滿常發生的

看一個解,

將byte array另外轉成Bitmap的作法如下

   Bitmap picture = takePictureDecode(imgData, camera);

    public Bitmap takePictureDecode(byte[] imgData, Camera camera) {
        BitmapFactory.Options opt;

        opt = new BitmapFactory.Options();
        opt.inTempStorage = new byte[16 * 1024];
        Parameters parameters = camera.getParameters();
        Size size = parameters.getPictureSize();

        int height11 = size.height;
        int width11 = size.width;
        float mb = (width11 * height11) / 1024000;

        if (mb > 4f)
            opt.inSampleSize = 4;
        else if (mb > 3f)
            opt.inSampleSize = 2;

        //preview from camera
        Bitmap bitmap = BitmapFactory.decodeByteArray(imgData, 0, imgData.length,opt); 
        return bitmap;
    }

應該是可以解決問題