java.lang.OutOfMemoryError照相机应用程序

java.lang.OutOfMemoryError照相机应用程序,java,android,camera,Java,Android,Camera,我有一个问题,我已经搜索了几个星期,仍然没有解决方案。 此应用程序用于拍摄带有边框的照片。框架是PNG格式的 当我启动应用程序时,一切都很顺利,但当我点击按钮拍照时,应用程序崩溃了。有时它给出了OfMemoryError,有时是ArrayIndexOfBoundsException,所以我几乎什么都试过了,但什么都没试过。 我应该改变抽丝的尺寸吗?或者我应该设置最终bmp的大小 我为我的英语感到抱歉,因为我是法国人。。 这是密码 private class CapturePictureCallb

我有一个问题,我已经搜索了几个星期,仍然没有解决方案。 此应用程序用于拍摄带有边框的照片。框架是PNG格式的

当我启动应用程序时,一切都很顺利,但当我点击按钮拍照时,应用程序崩溃了。有时它给出了OfMemoryError,有时是ArrayIndexOfBoundsException,所以我几乎什么都试过了,但什么都没试过。 我应该改变抽丝的尺寸吗?或者我应该设置最终bmp的大小

我为我的英语感到抱歉,因为我是法国人。。 这是密码

private class CapturePictureCallback implements PictureCallback {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10,
                data.length);

        final ViewFlipper flipper = (ViewFlipper) findViewById(R.id.main_flipper);
        View flipperView = flipper.getCurrentView();

        Bitmap flipperBitmap = Bitmap.createBitmap(flipperView.getWidth(), flipperView.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas flipperCanvas = new Canvas(flipperBitmap);
        flipperView.draw(flipperCanvas);
        Bitmap resizedFlipperBitmap = Bitmap.createScaledBitmap(flipperBitmap, 500, 300, true);

        // Insert image on top
        Bitmap overlaidBitmap = overlay(picBitmap, resizedFlipperBitmap);

        picBitmap.recycle();
        resizedFlipperBitmap.recycle();

        // Create file
        save(overlaidBitmap);
        overlaidBitmap.recycle();
        camera.startPreview();
    }

    private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
        Bitmap bmOverlay = Bitmap.createBitmap( 0,0, bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        return bmOverlay;
    }


    private void save(Bitmap bitmap) {
        File picturesFolder = null;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
            picturesFolder = new File(
                    Environment.getExternalStorageDirectory(), "Pictures");
        } else {
            picturesFolder = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        }

        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String dateString = sdf.format(date);
        String fileName = "kissthepresident" + dateString + ".jpeg";
        File pictureFile = new File(picturesFolder, fileName);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            bitmap.recycle();

            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
                ContentValues values = new ContentValues(7);

                values.put(Images.Media.TITLE, fileName);
                values.put(Images.Media.DISPLAY_NAME, fileName);
                values.put(Images.Media.DATE_TAKEN, dateString);
                values.put(Images.Media.MIME_TYPE, "image/jpeg");
                values.put(Images.Media.ORIENTATION, 0);
                values.put(Images.Media.DATA,
                        pictureFile.toString());
                values.put(Images.Media.SIZE, pictureFile.length());

                Uri uri = Uri.fromFile(pictureFile);
                getContentResolver().insert(uri, values);

            } else {
                MediaScannerConnection.scanFile(getApplicationContext(),
                        new String[] { pictureFile.toString() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.main_layout));
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof View) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

一个问题:解码字节数组(字节[]数据,整数偏移量,整数长度)

应该是(我不知道10的偏移量是多少,但你应该修正长度)


你试过调试你的代码吗?你知道是什么导致OutOfMemory错误吗?令人惊讶的是,有这么多人认为StackOverflow的用户喜欢免费调试其他人的代码。实际上,当我输入0时,我得到了内存错误,当我输入10时,我得到了另一个。。
FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:403)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
at com.kissthepresidents.Main$CapturePictureCallback.onPictureTaken(Main.java:255)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:529)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10,
            data.length);
Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10,
            data.length - 10);