From c600e6d0c5314b8180fce2f02f5927fb4ba10850 Mon Sep 17 00:00:00 2001 From: ojgarciab Date: Sat, 4 Apr 2015 01:06:16 +0200 Subject: [PATCH] Fix #3: Moved some code from onCreate to onResume --- AndroidCameraServer/res/values-es/strings.xml | 8 +++++++ AndroidCameraServer/res/values/strings.xml | 2 +- .../androidcameraserver/MainActivity.java | 24 ++++++++++--------- 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 AndroidCameraServer/res/values-es/strings.xml diff --git a/AndroidCameraServer/res/values-es/strings.xml b/AndroidCameraServer/res/values-es/strings.xml new file mode 100644 index 0000000..0919741 --- /dev/null +++ b/AndroidCameraServer/res/values-es/strings.xml @@ -0,0 +1,8 @@ + + + + AndroidCameraServer + Configuración + No se pudo inicializar la cámara + + diff --git a/AndroidCameraServer/res/values/strings.xml b/AndroidCameraServer/res/values/strings.xml index 3332f5a..6c9e6b3 100644 --- a/AndroidCameraServer/res/values/strings.xml +++ b/AndroidCameraServer/res/values/strings.xml @@ -2,7 +2,7 @@ AndroidCameraServer - Hello world! Settings + Camera couldn\'t be initialized \ No newline at end of file diff --git a/AndroidCameraServer/src/com/linaresdigital/android/androidcameraserver/MainActivity.java b/AndroidCameraServer/src/com/linaresdigital/android/androidcameraserver/MainActivity.java index bc2e690..9efd692 100644 --- a/AndroidCameraServer/src/com/linaresdigital/android/androidcameraserver/MainActivity.java +++ b/AndroidCameraServer/src/com/linaresdigital/android/androidcameraserver/MainActivity.java @@ -21,6 +21,7 @@ import android.util.Log; import android.view.Menu; import android.widget.FrameLayout; +import android.widget.Toast; public class MainActivity extends Activity { @@ -52,14 +53,6 @@ protected void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); /* Provide continuous autofocus if camera does not support it */ autoFocusHandler = new Handler(); - /* Get an instance of the default camera */ - mCamera = getCameraInstance(); - /* We create an instance of CameraPreview to manage the camera */ - mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); - /* Find the frame that will contain the camera preview */ - preview = (FrameLayout) findViewById(R.id.frameLayout); - /* Add view to frame */ - preview.addView(mPreview); /* Initialize ICE, copied from an example */ /**************************************************************************/ @@ -104,6 +97,8 @@ private void releaseCamera() { if (mCamera != null) { /* Disable callbacks */ mCamera.setPreviewCallback(null); + mCamera.stopPreview(); + mCamera.lock(); mCamera.release(); mCamera = null; /* Save the state */ @@ -178,20 +173,27 @@ public void onStop() { } public void onPause() { - super.onPause(); releaseCamera(); preview.removeAllViews(); mPreview = null; + super.onPause(); } @Override protected void onResume() { super.onResume(); - /* Create a fresh instance of CameraPreview to manage the camera */ + /* Get an instance of the default camera */ + mCamera = getCameraInstance(); + if (mCamera == null) { + Toast.makeText(getApplicationContext(), R.string.error_camera, + Toast.LENGTH_LONG); + this.finish(); + } + /* We create an instance of CameraPreview to manage the camera */ mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); /* Find the frame that will contain the camera preview */ preview = (FrameLayout) findViewById(R.id.frameLayout); - /* Add new view to frame */ + /* Add view to frame */ preview.addView(mPreview); }