5

Here is an interesting one...

I have an application I am writing for devices running android 2.3.3 and above.

It has a main activity which calls the camera via an Intent.

If the user clicks a button to launch the camera; then takes a picture; then clicks "Done" to return to the main activity - the application works fine and displays the new picture in an imageview on the main activity.

however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then click done to return to the main activity - the application crashes

but then...however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then changes the orientation back to portrait (in the camera) before clicking done to return to the main activity - then the application continues to run normally.

Stuff I have tried: I have changed the manifest file to force the application (main activity) to be oriented in portrait (I have also removed this) I have added this line to the main activity in an attempt to handle the re-drawing of the activity on it return from the camera intent:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

In the debug window I get errors describing nullpointer exceptions - I think this is because the views are no longer there after an orientation change for the image to be passed into unless the orientation is the same as when it was left.

I'm a bit stuck so would appreciate some advice.

agrodude
  • 55
  • 1
  • 7

3 Answers3

3

When your screen orientation changes, your Activity is destroyed and recreated in the new orientation. So any variables that gained a reference during the life of the Activity will no longer refer to anything, and if you then try to access objects they refer to without re-assigning them values, you'll get a NullPointerException.

The method onSaveInstanceState() is used to save temporary data between configuration changes. This creates a Bundle which is passed to onCreate() when the Activity starts up again.

Without any code, I don't know if that's your problem, but it's worth a look.

See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for more information (and more accurate information than I've provided, no doubt).

Spinner
  • 732
  • 1
  • 6
  • 21
  • Thanks for the tip. During my investigation, I actually found two crashes that I had to deal with. The first crash was resolved by inserting these two methods into my main activity: – agrodude Jul 11 '12 at 09:20
2

Some Android devices like Samsung S3 and S4 have default camera surface view in Landscape mode. When you call camera and capture image and come to your application, whether you had set orientation PORTRAIT it will first open in LANDSCAPE mode and then changes to PORTRAIT mode.

Its Android OS behaviour. Due to this Activity Re-Creates it self. At that time you are getting Null Pointer Exception. You can handle by setting configChanges in menifest file.

android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"

and you can store the data temporary in onSaveInstanceState() rightly said by @Spinner in his answer.

Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
1

hey i am not sure but try to put this property in manifest file hopefully it should work

android:configChanges="orientation|keyboardHidden" 
  • That would probably work, but it's not recommended by Google as anything other than a last resort - see http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange – Spinner Jun 21 '12 at 12:33
  • @DroidWormNarendra tell me what exactly you want to do with soft keyboard? –  Aug 06 '15 at 14:39
  • @PriyankBhojak I don't want to do anything with soft keyboard. I just wanted to prevent the screen rotation, that happens right after capturing and saving image. – Narendra Singh Aug 06 '15 at 16:00
  • @DroidWormNarendra if you are using Camera with SurfaceView then you can add this line : camera.setDisplayOrientation(90); in your surfaceChanged method. and if you are using native camera than you can user ExifInterface see here : http://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image, You should not down vote answer if someone trying to help you. –  Aug 07 '15 at 05:33
  • 1
    @PriyankBhojak, I used your solution, its working fine. Great one ! – Hiren Patel Aug 07 '15 at 05:36
  • @PriyankBhojak I am using native camera...and using ExifInterface will help me correcting the bitmap orientation, but my trouble is....I want to avoid screen rotation (thats for a moment), faced after coming back from camera screen after capturing and saving the pic. – Narendra Singh Aug 07 '15 at 07:28
  • I also noticed, the issue is happening when we capture photo keeping the phone portrait. but doing the same in landscape mode, works fine. – Narendra Singh Aug 07 '15 at 07:28