0

I have built a custom camera application . Facing a problem in screen orientation and I don't want the activity to be re-created when the orientation changes. Please help me on this with the complete solution.

Getting a NULL pointer exception inside the surfaceChanged method in the preview class.

  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        if (mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters();
            Log.d("SurfaceChanged", "getting parameters" + parameters);

Getting a NULL pointer exception in the below line :

        Display display =                               ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Log.d("SurfaceChanged", "Getting the system services");
        if(display.getRotation() == Surface.ROTATION_0){
            Log.d("SurfaceChanged", "Inside the if condition 1");
            parameters.setPreviewSize(height,width);
            Log.d("SurfaceChanged", "setting preview size");
            mCamera.setDisplayOrientation(90);
            Log.d("SurfaceChanged", "setting display orientation as 90");
        }

        if(display.getRotation() == Surface.ROTATION_90){
            Log.d("SurfaceChanged", "Inside the if condition 2");
            parameters.setPreviewSize(width,height);
            Log.d("SurfaceChanged", "setting the preview size 2 ");
        }

        if(display.getRotation() == Surface.ROTATION_180){
            Log.d("SurfaceChanged", "Inside the if condition 3");
            parameters.setPreviewSize(height,width);
            Log.d("SurfaceChanged", "setting the preview size 3");
        }

        if(display.getRotation() == Surface.ROTATION_270){
            Log.d("SurfaceChanged", "Inside the if condition 4");
            parameters.setPreviewSize(width,height);
            Log.d("SurfaceChanged", "setting preview size 4");
            mCamera.setDisplayOrientation(180);
            Log.d("SurfaceChanged", "setting display orientation as 180");
        }

        mCamera.setParameters(parameters);
        Log.d("SurfaceChanged", "setting the parameters");
       try {
            mCamera.setPreviewDisplay(mHolder);
            Log.d("SurfaceChanged", "setting preview display to mHolder");
            mCamera.startPreview();
            Log.d("SurfaceChanged", "preview as started");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Any help would be greatly appreciated.

Here is the error log:

12-23 16:07:34.962 8845-8845/com.clarity.camera E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.clarity.camera , PID: 8845 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at com.clarity.camera.CameraPreview.surfaceChanged(CameraPreview.java:230) at android.view.SurfaceView.updateWindow(SurfaceView.java:583) at android.view.SurfaceView.access$000(SurfaceView.java:86) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Nompumelelo
  • 929
  • 3
  • 17
  • 28
Likith Ts
  • 296
  • 1
  • 7
  • 18

1 Answers1

1

If you don't want your Activity to be destroyed you can place this in your activity tags in you manifest file:

android:configChanges="orientation|screenSize|keyboardHidden"

This will tell Android that you are going to handle orientationChanges by yourself. screenSize is important if you are developing on Android API 13 or higher. If you are developing for Android API 12 or lower you can leave screenSize out!

That' means onDestroy will not be called anymore. Instead onConfigurationChanged is called and you can handle what ever you want to do in this method when a Rotation of the device occurs.

See here: http://developer.android.com/guide/topics/resources/runtime-changes.html

TO help you with the NullPointerException you will Need to post your LogCat Output and tell us which line exactly throws the NPE!

Mike
  • 857
  • 1
  • 7
  • 11
  • E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: PID: 8845 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at CameraPreview.surfaceChanged – Likith Ts Dec 23 '14 at 10:55
  • @LikithTs Post your whole logcat into your question at the bottom! – Mike Dec 23 '14 at 10:57
  • Can I call onConfigurationChanged in custom camera application. If it is possible what should I handle inside this method. – Likith Ts Dec 23 '14 at 10:58
  • You can call onConfigurationChanged in your `Activity`! This method will be fired everytime a orientation Change occurs! Everything you want to do on a configuration Change. If there is nothing for you to do simply don't `Override` it. But due to that Android doesn't destroy your `Activity` anymore it can happen that the `Layout` isn't the way you want after a orientation Change. That means you maybe Need to do the `layouting` by yourself when a orientation Change occurs! Hope it helps! Just try it ;) Or read more in the link i posted in the question! – Mike Dec 23 '14 at 11:01
  • I'll accept your answer. I'm using setDisplayOrientation(90) inside my camera preview class. If i'm not using this my preview is rotated 90 degree. Please help me in orientation part. If I can read the device orientation. It will be easy for me to rotate the photo and save the photo in gallery. – Likith Ts Dec 23 '14 at 11:24
  • Whenever my photo is saved in gallery all the photos which is taken in portrait is rotated to landscape and saved.But when rotate the photo before saving to the gallery. Even landscape image is saving vertically but horizontal view. So I required to read the orientation of the device and save for the photo as all inbuilt camera app dose. – Likith Ts Dec 23 '14 at 11:29
  • @LikithTs i think this is exactly what you are looking for. In this link see the answere where he describes how to detect which orientation the device has: http://stackoverflow.com/questions/5726657/how-to-detect-orientation-change-in-layout-in-android – Mike Dec 23 '14 at 11:36