Lock Camera orientation to portrait

2.6k views Asked by At

How can I prevent the android camera to switch orientation to landscape if I start the camera like this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFolder = new File (Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
    imageFile = new File (imageFolder, UUID.randomUUID().toString()+".png");
    Uri uriImage = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
    startActivityForResult(intent, 0);

So I don't have a particular camera activity. In the manifest.xml the whole appliaction is set to portrait orientation, but the camera switches.

Second Problem, after taking picture and setting it to the imageView it ist switched in orientatien even if I took it in portrait mode (I save the image before I set the imageView), how can I display it in the right posititon?

1

There are 1 answers

2
Jignesh Jain On BEST ANSWER

try this for lock camera orientation to portrait,

// *************************************************************************//
    // Stop the screen orientation changing during an event
    // *************************************************************************//

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
    }

    private void lockScreenRotation(int orientation)
    {
        // Stop the screen orientation changing during an event
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    // *************************************************************************//
    // store the file url as it will be null after returning from camera app
    // *************************************************************************//

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        // save file url in bundle as it will be null on scren orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

Set this on onActivityResult for getting picture in right position

if (resultCode == Activity.RESULT_OK
                && requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
        {
            filePath = fileUri.getPath();

            Utility.log("FILE PATH CAMEREA:->", filePath);
            // AppSharedPrefrence.getInstance(getActivity()).setImagePath(path);

            // *************************************************************************//
            // set default camera rotation
            // *************************************************************************//
            try
            {
                File f = new File(filePath);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
                {
                    angle = 90;
                }
                else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
                {
                    angle = 180;
                }
                else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
                {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;

                Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                Bitmap bmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);
                OutputStream stream = new FileOutputStream(filePath);
                bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);

                // imageBmp =
                // Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path),
                // 400, 400, true);
                imageBmp = BitmapFactory.decodeFile(filePath);
                imgProfilePic.setImageBitmap(imageBmp);

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (OutOfMemoryError oom)
            {
                oom.printStackTrace();
            }

        }