Android camera intent and orientation issue

I had the following problem when starting the camera in an Android application: if the application starts in orientation, say, it depicts, the camera only works in the image. If you change the orientation of the phone to landscape, when you start the application or start the camera, and try to make a landscape picture, the power of the application will close and with an error. This is similar to the fact that the intent of the camera can only be shot in the orientation that the application was at startup.

I can take pictures in the landscape ONLY if I start my application in the landscape and photograph only in the portrait, only if I start it in portait. The code to trigger camera intent is pretty 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 have nothing to do with the orientation of the camera in the manifest, nor ...

Greetings

Aram

+3
source share
5 answers

I solved this by putting configChanges="orientation"in an action that triggers the camera. Hope that helps :)

+6
source

Most telephone cameras are landscapes, which means that if you take a photo in a portrait, the photos you take will be rotated 90 degrees. In this case, the camera software should fill in the EXIF ​​data with the orientation in which the photo should be viewed.

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 method rotateImage:

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;
}
+1
source

, onActivityResult. potrait, , , outputFileUri, , NullPointerException.

0

, , -, . , ResultOK, , != Null, fileUri!= Null, , data.getData() Uri, . , .

0

I believe the problem is that the value of outputFileUri becomes null after changing the orientation of the activity.

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

@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 helps anyone with this problem!

0
source

All Articles