Camera preview screen black when activity resumes

10,424

In your onResume(), you are calling:

mCamera = Camera.open();
mCamera = getCameraInstance();

Here what is happening, is you are getting a camera instance through Camera.open() and after this statement, you are again trying to get the camera instance by calling getCameraInstance(); which will return null coz your activity has already got the instance and hence mCamera instance is getting null.

If this doesn't solves your problem, please tell me, I will see for some other solution.

Share:
10,424
RedChris
Author by

RedChris

Updated on June 04, 2022

Comments

  • RedChris
    RedChris almost 2 years

    I've been trying to get the camera section on my app working for about a week now, and after finally fixing a bug, it has created a new one.

    When I resume the activity, the preview doesn't work and neither does the recording functions. By looking at Logcat, I found out my Camera object is equal to null and I can't find a away to recreate it.

    My On Create

    mCamera = getCameraInstance();    
    // Create preview view and set it as the content of our activity.    
    mPreview = new CameraPreview(this, mCamera);    
    preview = (FrameLayout) findViewById(R.id.previewer);   
    preview.addView(mPreview);
    

    Get Camera Instance Method

    private Camera getCameraInstance() { 
    
            Camera c = null;     
            try {     
                //c = Camera.open(); // attempt to get a Camera instance    
                 c = this.open(); // attempt to get a Camera instance    
            } catch (Exception e) {     
                // Camera is not available (in use or does not exist)    
            }     
            Log.i("TEST getCameraInstance", "" + c);     
            return c; // returns null if camera is unavailable          
    }
    

    My On Pause

    protected void onPause() {
    
            super.onPause();      
            try {     
                // release the camera immediately on pause event     
                // releaseCamera();    
                mCamera.stopPreview();      
                mCamera.setPreviewCallback(null);    
                mCamera.release();     
                mCamera = null;     
            } catch (Exception e) {       
                e.printStackTrace();    
            }    
    }
    

    My on Resume

    protected void onResume() {
    
            super.onResume();       
            try {       
                mCamera = Camera.open();      
                mCamera = getCameraInstance();     
                Log.i("TEST onresume", "onResume Runing");      
                Log.i("TEST onresume", "camera "  + mCamera);       
                mCamera.setPreviewCallback(null);      
                Log.i("TEST onresume", "camera "  + mCamera);      
                //mCamera = getCameraInstance();      
                Log.i("TEST onresume", "camera "  + mCamera);    
                // mCamera.setPreviewCallback(null);     
                mPreview = new CameraPreview(MainRecordScreen.this, mCamera);// set preview      
                Log.i("TEST onresume", "camera "  + mCamera);                                                                               
                preview.addView(mPreview);     
                Log.i("TEST onresume", "camera "  + mCamera);    
                mCamera.startPreview();              
            } catch (Exception e) {      
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());    
            }      
            Log.i("TEST onresume", "camera "  + mCamera);    
    }
    

    My LogCat

    06-19 06:38:40.223: E/AndroidRuntime(16702): FATAL EXCEPTION: main
    06-19 06:38:40.223: E/AndroidRuntime(16702): java.lang.NullPointerException
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen.prepareVideoRecorder(MainRecordScreen.java:198)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen.access$7(MainRecordScreen.java:193)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen$3.onClick(MainRecordScreen.java:115)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.view.View.performClick(View.java:3511)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.view.View$PerformClick.run(View.java:14105)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Handler.handleCallback(Handler.java:605)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Handler.dispatchMessage(Handler.java:92)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Looper.loop(Looper.java:137)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.app.ActivityThread.main(ActivityThread.java:4424)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at java.lang.reflect.Method.invoke(Method.java:511)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    06-19 06:38:40.223: E/AndroidRuntime(16702):    at dalvik.system.NativeStart.main(Native Method)
    

    mCamera returns null from the getCameraInstance() method if it helps.

  • RedChris
    RedChris almost 12 years
    Thank you, removing getCameraInstance(); got it working perfectly straight away.