-->

Android Camera Intent - Out Of Memory Error and Ro

2019-05-10 00:58发布

问题:

I'm creating an android app that utilizes the camera to take pictures and then sends them to a server based on user input. I'm currently having some problems with the camera intent. My main problems are :

  1. Getting a picture that seems to be rotated when compared to the position it was taken.

  2. When I try to fix this Rotation I get an OutOfMemoryError

So mainly I need to make sure that the orientation of the picture doesn't change and that I don't get an OutOfMemoryError.

Here is the function that uses the camera Intent to take the picture.

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);
    }

And this function is used to rotate the image and save it at that rotation.

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

    }

And finally, this is my onActivityResult method :

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()));
    }

I've been trying to solve this problem for a long time and I would appreciate it if I could get some help. Thanks!

回答1:

A way to fix your out of memory error would be to use Bitmap.Factory Options, this has an option that will allow you to decode the image as a scaled image so it won't use quite so much memory, the downside to this, is that the resulting image will be somewhat smaller. Its a setting that you could play with and potentially fine tune to get it to an acceptable loss. Also you will want to call p.dispose() on your first bitmap immediately after you have created your second bitmap. Follow this link for an example: http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html