4

我有一个问题/问题,我一直在寻找几个星期,但仍然没有解决方案。此应用程序用于拍摄带有框架的照片。框架是 PNG 格式。

当我启动应用程序时,一切都很好,但是当我单击按钮拍照时,应用程序崩溃了。有时它会给出 OutOfMemoryError,有时会给出 ArrayIndexOutOfBoundsException,所以我几乎尝试了所有方法,但什么也没做。我应该改变drawbles的大小吗?还是我应该设置最终 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();
    }
}

这是目录

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

1 回答 1

1

问题一: decodeByteArray(byte[] data, int offset, int length)

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

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

Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10,
            data.length - 10);
于 2012-10-23T18:53:01.400 回答