Aram Alvarez
Aram Alvarez

Reputation: 566

Problem with Android camera intent & orientation

I'm having the following problem when launching the camera intent in my Android application: if the application is launched in an orientation, say, portray, the camera only works in portray. If you change the phone orientation to landscape, with the application running, or the camera intent running, and try to take a landscape picture, the application force closes and with an error. It is like if the camera intent is able to shoot only in the orientation that the application was when launched.

I can take pictures in landscape ONLY if I start my application in landscape, and take pictures in portrait ONLY if I start it in portait. The code to launch the camera intent is quite simple:

        String path = (new StringBuilder()).append(Environment.getExternalStorageDirectory()).append("/"+fileName).toString();
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra("output", outputFileUri);
        startActivityForResult(intent, CAMERA_RESULT);

Any ideas why this is happening? I don't have anything related to camera orientation in the manifest neither...

Cheers

Aram

Upvotes: 3

Views: 7132

Answers (7)

Legit007
Legit007

Reputation: 193

So what helped me was

ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize

Source: https://learn.microsoft.com/en-us/answers/questions/1163753/xamarin-android-app-restarting-on-orientation-chan

Upvotes: 0

oppicool
oppicool

Reputation: 11

add this in the activity Tag (AndroidManifest.xml)

android:configChanges="orientation|keyboardHidden"

Upvotes: 0

David
David

Reputation: 11

I believe the issue is that the outputFileUri value becomes null once the orientation of the activity is changed.

I was able to solve this problem by adding a saved instance state for the variable like this:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if(outputFileUri!=null) {
        outState.putString("outputFileUri", outputFileUri.toString());
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    outputFileUri = Uri.parse(savedInstanceState.getString("outputFileUri"));
}

Hope this can help anybody with this issue!

Upvotes: 1

Shirish Herwade
Shirish Herwade

Reputation: 11701

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the EXIF data with the orientation that the photo should be viewed in.

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

switch(orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotateImage(bitmap, 90);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotateImage(bitmap, 180);
        break;
    // etc.
}

Here is the rotateImage method:

public static Bitmap rotateImage(Bitmap source, float angle) {
    Bitmap retVal;

    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    return retVal;
}

Upvotes: 1

Anjan Debnath
Anjan Debnath

Reputation: 1

Ok, everybody, here is something that worked for me. In android developers they have given inside the ResultOK block to check if data!=null but if you change it with fileUri!=null, it works fine and later also while saving the file one should replace data.getData() with fileUri, it saves the image as well. Hope this helps.

Upvotes: 0

captainDizzy
captainDizzy

Reputation: 853

I solved this by putting configChanges="orientation" in the activity that calls the camera. Hope that helps :)

Upvotes: 5

user622570
user622570

Reputation: 9

I think the problem is in your onActivityResult function. If you start the camera intent in potrait mode, then try to rotate the screen and take a landscape photo, when your 1st activity resume, It lost the outputFileUri that you wanted to take, make it null and throw out the NullPointerException Error.

Upvotes: 0

Related Questions