Android相机意图 - 内存不足错误和旋转错误

3
我正在创建一款安卓应用程序,利用相机拍照,并根据用户输入将照片发送到服务器。目前我遇到了一些相机意图的问题,主要问题如下:
  1. 拍摄的照片在显示时似乎被旋转了。
  2. 当我尝试修正这个旋转时,出现了OutOfMemoryError错误。
因此,我的主要任务是确保照片的方向不会改变,并且不会出现OutOfMemoryError错误。
下面是使用相机意图拍照的函数:
private void dispatchTakePictureIntent(int actionCode) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(takePictureIntent, actionCode);
    }

这个函数用于旋转图像并将其保存在该旋转角度下。

private void getRotate() {

        String imagePath ="";
        int rotate = 0;
        File f = null;
        try {
             f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
             imagePath = f.getAbsolutePath();
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Bitmap p = BitmapFactory.decodeFile(imagePath);

        Matrix m = new Matrix();
        m.setRotate(rotate);
        Bitmap _bitmapScaled = Bitmap.createBitmap(p, 0, 0, p.getWidth()/2,p.getHeight(), m, false);
        f.delete();



        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 100, bytes);





        //you can create a new file name "test.jpg" in sdcard folder.
        File f1 = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
        try {
            f1.createNewFile();
            FileOutputStream fo = new FileOutputStream(f1);
            fo.write(bytes.toByteArray());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //write the bytes in file

    }

最后,这是我的onActivityResult方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        getRotate();
        File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
        mImageView.setImageBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()));
    }

我已经尝试了很长时间来解决这个问题,如果可以得到一些帮助,我将不胜感激。谢谢!

1个回答

5
使用Bitmap.Factory Options是解决内存不足错误的一种方法,它提供了一个选项,可以将图像解码为缩小的图像,从而减少内存使用量。但是,这样做的缺点是生成的图像会比较小。您可以尝试调整设置,以获得可接受的损失。此外,在创建第二个位图后,您需要立即调用p.dispose()释放第一个位图。点击以下链接查看示例:http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接