仅在Galaxy Note 3中出现内存不足错误

时间:2014-03-27 22:22:08

标签: android android-intent camera imageview

我正在开发一个应用程序,可以使用额外裁剪选项使用相机意图显示来自相机的照片。代码在大多数设备上工作正常,但是当我尝试在我的全新Galaxy Note3中测试时,它崩溃并且没有正常运行,而且所拍摄的图像仍然很大并且#34;差不多4 MB& #34;这是如此之大,以至于在imageview中显示。如果无论如何都可以指望我避免这种情况吗?

下面是我的代码:

Intent intent = new Intent(
                                        "android.media.action.IMAGE_CAPTURE");
                                file = getOutputMediaFile();
                                intent.putExtra("crop", "true");
                                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        Uri.fromFile(file));
                                intent.putExtra("outputFormat",
                                        Bitmap.CompressFormat.JPEG
                                                .toString());
                                intent.putExtra(
                                        MediaStore.EXTRA_SCREEN_ORIENTATION,
                                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

                                startActivityForResult(intent,
                                        ACTION_REQUEST_CAMERA);

和activityforresult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        switch (requestCode) {

        case ACTION_REQUEST_CAMERA:

            if (data != null) {

                try {
                    int inWidth = 0;
                    int inHeight = 0;

                    InputStream in = new FileInputStream(
                            file.getAbsolutePath());

                    // decode image size (decode metadata only, not the
                    // whole image)
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeStream(in, null, options);
                    in.close();
                    in = null;

                    // save width and height
                    inWidth = options.outWidth;
                    inHeight = options.outHeight;

                    // decode full image pre-resized
                    in = new FileInputStream(file.getAbsolutePath());
                    options = new BitmapFactory.Options();
                    // calc rought re-size (this is no exact resize)
                    options.inSampleSize = Math.max(inWidth / 350,
                            inHeight / 550);
                    // decode full image
                    Bitmap roughBitmap = BitmapFactory.decodeStream(in,
                            null, options);

                    // calc exact destination size
                    Matrix m = new Matrix();
                    RectF inRect = new RectF(0, 0, roughBitmap.getWidth(),
                            roughBitmap.getHeight());
                    RectF outRect = new RectF(0, 0, 700, 800);
                    m.setRectToRect(inRect, outRect,
                            Matrix.ScaleToFit.CENTER);
                    float[] values = new float[9];
                    m.getValues(values);

                    // resize bitmap
                    Bitmap resizedBitmap = Bitmap.createScaledBitmap(
                            roughBitmap,
                            (int) (roughBitmap.getWidth() * values[0]),
                            (int) (roughBitmap.getHeight() * values[4]),
                            true);

                    // save image
                    try {
                        FileOutputStream out = new FileOutputStream(
                                file.getAbsolutePath());
                        resizedBitmap.compress(Bitmap.CompressFormat.JPEG,
                                90, out);

                        fullphoto = resizedBitmap;

                        setPic(file.getAbsolutePath(), camera);

                    } catch (Exception e) {
                        Log.e("Image", e.getMessage(), e);
                    }
                } catch (IOException e) {
                    Log.e("Image", e.getMessage(), e);
                }
            }

        //  fullphoto = BitmapFactory.decodeFile(file.getAbsolutePath());

            // photo = decodeSampledBitmapFromFile(file.getAbsolutePath(),
            // 100, 100);

        //  camera.setImageBitmap(imghelper.getRoundedCornerBitmap(
        //          fullphoto, 10));

            iscamera = "Yes";
            firsttime = false;

            break;

}

0 个答案:

没有答案