简体   繁体   中英

Custom Camera Orientation Issue

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.v iew.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)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM