3

I am creating an application and part of it is where the user can change their profile picture either by taking a picture or using one from the native gallery.

So I'm using the native camera and I'm using the following lines of code to open the camera view

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image_saved_destination));
        startActivityForResult(intent, CAMERA_REQUEST_CODE);

So with this it keeps overwriting the same file on the external storage.

This was working fine until I updated my os on one of my devices to lollipop. Now when I use the camera if I keep it in held in portrait and take the pic and save the orientation when it returns to my app flips from landscape to portrait. However, when the camera is open and if I hold the phone in landscape and take the pic and click save, it returns to the app with no orientation abnormalities.

Due to the orientation being changed and changed back I lose the state of the activity so my variables etc are lost, which are needed for processing the image data.

Has anyone encountered this?

DJ-DOO
  • 4,219
  • 15
  • 48
  • 91
  • "Due to the orientation being changed and changed back I lose the state of the activity so my variables etc are lost, which are needed for processing the image data" -- that's a bug in your app that you need to fix. This will affect you for other sorts of configuration changes as well, plus some cases where the user returns to your app via the recent-tasks list. Use `onSaveInstanceState()` or a retained fragment. – CommonsWare May 27 '15 at 19:48
  • @CommonsWare thanks for your response, I had tried that but unfortunately I have a file initialised which (as far as I'm aware) can't add to a bundle. The problem also is when it returns from the camera intent, the activity is in landscape and then reverts back to portrait which looks wrong, and the orientation is already specified as portrait. – DJ-DOO May 28 '15 at 10:28
  • I have no idea what "a file initialised" means. There are many configuration changes beyond screen rotation, such as the user changing locale, the user putting the device in a car dock, etc. And, as noted previously, there are other scenarios you need to handle, such as your app's process being terminated in the background and the user returning to it from a recent task. You may wish to ask a separate Stack Overflow question to determine how best to handle these configuration changes. – CommonsWare May 28 '15 at 10:39
  • In terms of the "looks wrong", there are thousands of Android camera apps, both pre-installed on various devices and available through channels like the Play Store. The user could choose any of them for handling your `Intent`. Blaming the third-party app because the transition from it "looks wrong" is impractical. You are going to need to change the behavior of **your** app, either so the transition no longer "looks wrong", or to stop using third-party apps for taking pictures on your behalf. – CommonsWare May 28 '15 at 10:41
  • @CommonsWare ok, no need to get so angry. I was looking for advice, not a lecture. I do appreciate you are very worldly wise when it come to android, however, the fact remains the camera intent changes the orientation of my activity and when **My** App returns to said activity it is in landscape and then reverts to portrait, so It does look wrong, and this wasn't an issue pre lollipop for me so I was just asking if anyone had encountered this issue. The fact also remains that some of us are still learning. Thanks for your help. – DJ-DOO May 28 '15 at 10:49

2 Answers2

3

The ACTION_IMAGE_CAPTURE intent and the rotation between activity changes, are not related to the new Android camera2 API at all. This is just standard behavior of Android's intent system and switching between activities.

It sounds like the new OS comes with a new version of the native camera app, which has changed its behavior in terms of managing screen orientation. You should look into the standard approaches with managing orientation and other configuration changes: http://developer.android.com/guide/components/activities.html#ConfigurationChanges

Those will allow you to either handle the orientation change yourself instead of the OS restarting your activity, or allow you to stash your private data into a bundle that is given to you in onCreate.

Eddy Talvala
  • 15,449
  • 2
  • 37
  • 42
  • 2
    Thanks for your response. I have tried these methods since posting, but I also have an initialised file that gets lost in the orientation change and also when the app returns from the camera intent, the activity is in landscape and then reverts back to it's specified orientation (portrait), this is visiible to the user and looks very bad. Also, just a quick addition, this wasn't an issue with the camera intent before upgrading to lollipop. I am testing on a s4 and s3mini and the s3 mini which is pre lollipop doesn't experience this issue, nor did the S4 until upgrade – DJ-DOO May 28 '15 at 10:31
-1

Better You create your own custom camera using camera API.If you encounter the same problem again then you can use setDisplayOrientation of camera preview.for more visit Android - Camera preview is sideways

Community
  • 1
  • 1
Sanju Talreja
  • 39
  • 2
  • 7
  • This comment isn't really addressing the issue. The problem is surrounding the new camera2 added into Lollipop. Visually the orientation of the camera preview is correct. – DJ-DOO May 27 '15 at 09:41