Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong></p> <p>This is a tough one to answer, I can't be 100% sure what's causing your exceptions right now. I have included my Camera code below, hopefully that will help, I use <code>startCamera()</code> and <code>stopCamera()</code> methods that I call in <code>onPause</code> and <code>onResume</code>. I also, only create a new instance of the <code>CameraPreview</code> class in my <code>onCreate</code>, I don't re-instantiate in <code>onResume</code> unless my <code>cameraView == null</code>. There are a couple things we do differently. Hopefully the code below will help, maybe you can play with it to get yours working:</p> <p><em>P.S.: Everything works for me. i.e. Going into other Activities, etc. The only part of the Lifecycle I haven't tested is <code>onDestroy</code>, but that is because my application is designed to start at the beginning in this cycle.</em></p> <p><strong>MainActivity:</strong></p> <pre><code>boolean cameraReleased = false; @Override protected void onPause() { Log.i("onPause", "CALLED:: cameraReleased = " + cameraReleased); Log.i("onResume", "CALLED:: cameraView = " + cameraView.toString()); if (cameraReleased == false) { image = null; imageResult.setImageBitmap(null); imageResult.setImageResource(0); cameraView.stopCamera(); cameraReleased = true; } if (cameraView == null) { Log.i("onPause", "cameraView == null"); cameraView = new JJCameraSurfaceView(getApplicationContext()); imageResult = new ImageView(getApplicationContext()); } super.onPause(); } @Override protected void onDestroy() { Log.e("onDestroy", "INITIATED"); super.onDestroy(); } @Override protected void onResume() { Log.i("onResume", "CALLED:: cameraReleased = " + cameraReleased); Log.i("onResume", "CALLED:: cameraView = " + cameraView.toString()); if (cameraReleased == true) { image = null; imageResult.setImageBitmap(null); imageResult.setImageResource(0); cameraView.startCamera(); } if (cameraView == null) { Log.i("onResume", "cameraView == null"); cameraView = new JJCameraPreview(getApplicationContext()); imageResult = new ImageView(getApplicationContext()); } super.onResume(); } @Override public void onBackPressed() { // If Statement used to get out of my camera view and back to my MainActivity - Same Class if (“Camera Preview or Image Result is displayed”) { cameraView.stopCamera(); image = null; imageResult.setImageBitmap(null); imageResult.setImageResource(0); cameraView.startCamera(); return; } Log.i("onBackPressed", "WAS PRESSED"); super.onBackPressed(); } </code></pre> <p><strong>CameraPreview:</strong></p> <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.w("surfaceChanged", "STARTED"); if (camera != null) { Log.w("surfaceChanged", "camera = NOT NULL"); Camera.Parameters cParams = camera.getParameters(); cParams.setPreviewSize(width, height); cParams.setSceneMode(Parameters.SCENE_MODE_NIGHT); camera.setParameters(cParams); camera.startPreview(); } } public void surfaceCreated(SurfaceHolder holder) { Log.w("surfaceCreated", "STARTED"); if (camera == null) { camera = Camera.open(); } try { camera.setPreviewDisplay(mHolder); } catch (Exception e) { Log.e("setPreviewDisplay", "FAILED: " + e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { Log.w("CameraSurfaceDestroyed", "INITIATED"); camera.stopPreview(); camera.release(); camera = null; } public void startCamera() { Log.w("startCamera", "CALLED"); mHolder = getHolder(); surfaceCreated(mHolder); camera.startPreview(); mHolder.addCallback(this); } public void stopCamera() { mHolder = getHolder(); mHolder.removeCallback(this); camera.stopPreview(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload