Return from Camera Intent

I have an Activity that fires a camera intent like this (nothing special):

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  filename);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    Uri imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, CAMERA_REQUEST);

Than I get the result in onActivityResult()and set the bitmap to ImageView. Again, nothing special.

The problem is that the Camera app is always in the landscape. Therefore, if the user holds the device in horizontal orientation, when they click "OK" to send it back to my activity, and my activity was previously in the portrait, then it resets my activity, because it needs to rebuild it. If you tilt the device to portrait orientation before you press OK in the camera, this will not work. How do I get around this?

+1
source share
3 answers

You need to override the method onSaveInstanceStateto save your activity settings before turning. Then in the onCreate method you can check if the argument is Bundlenull. If not, then you recreate your activity and must download saved settings from it.

+4
source

A quick and dirty route will be to change your orientation yourself. If your application does not have another layout for landscape and portrait, then this is a good route. Here is how.

Add android:configChanges="orientation"to your activity at AndroidManifest.

, , , onSaveInstanceState , , onRestoreInstanceState. LogCat, , NullPointerException, . .

+4

, , . , android:screenOrientation="portrait" .

, , .

0
source

All Articles