0
votes

How can I get device's rotation even with android screen rotation off. I am using Android's Camera API, and I need to take landscape and portrait photos even rotation off (image bellow with the functionality I'm talking about).

enter image description here

Is there any way to get the orientation of the device with this thing off?

2

2 Answers

0
votes

Yes, you can set the screen orientation programatically anytime you want using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

for landscape and portrait mode respectively. The setRequestedOrientation() method is available for the Activity class, so it can be used inside your Activity.

And this is how you can get the current screen orientation and set it adequatly depending on its current state:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
final int orientation = display.getOrientation(); 
 // OR: orientation = getRequestedOrientation(); // inside an Activity

// set the screen orientation on button click
Button btn = (Button) findViewById(R.id.yourbutton);
btn.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {

              switch(orientation) {
                   case Configuration.ORIENTATION_PORTRAIT:
                       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                       break;
                   case Configuration.ORIENTATION_LANDSCAPE:
                       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                       break;                   
               }
          }
   });

Also, you can get the screen orientation using the Configuration:

Activity.getResources().getConfiguration().orientation
0
votes

If you are using Camera API, you need to change rotation using camera parameters

Camera.Parameters param = mCamera.getParameters();
param.setRotation(90);
return param;

You can try different rotations degree in order to have exactly what you need. Try 90,180,270. Hope this helps.