Skip to content

Commit b3db467

Browse files
committed
Merge pull request #8 from AndroidCameraServer/Fix_6
Fix #3: Application crashes in onResume
2 parents a8845d9 + 0ef5549 commit b3db467

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">AndroidCameraServer</string>
5+
<string name="menu_settings">Configuración</string>
6+
<string name="error_camera">No se pudo inicializar la cámara</string>
7+
8+
</resources>

AndroidCameraServer/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<resources>
33

44
<string name="app_name">AndroidCameraServer</string>
5-
<string name="hello_world">Hello world!</string>
65
<string name="menu_settings">Settings</string>
6+
<string name="error_camera">Camera couldn\'t be initialized</string>
77

88
</resources>

AndroidCameraServer/src/com/linaresdigital/android/androidcameraserver/MainActivity.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.util.Log;
2222
import android.view.Menu;
2323
import android.widget.FrameLayout;
24+
import android.widget.Toast;
2425

2526
public class MainActivity extends Activity {
2627

@@ -52,14 +53,6 @@ protected void onCreate(Bundle savedInstanceState) {
5253
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
5354
/* Provide continuous autofocus if camera does not support it */
5455
autoFocusHandler = new Handler();
55-
/* Get an instance of the default camera */
56-
mCamera = getCameraInstance();
57-
/* We create an instance of CameraPreview to manage the camera */
58-
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
59-
/* Find the frame that will contain the camera preview */
60-
preview = (FrameLayout) findViewById(R.id.frameLayout);
61-
/* Add view to frame */
62-
preview.addView(mPreview);
6356

6457
/* Initialize ICE, copied from an example */
6558
/**************************************************************************/
@@ -104,6 +97,8 @@ private void releaseCamera() {
10497
if (mCamera != null) {
10598
/* Disable callbacks */
10699
mCamera.setPreviewCallback(null);
100+
mCamera.stopPreview();
101+
mCamera.lock();
107102
mCamera.release();
108103
mCamera = null;
109104
/* Save the state */
@@ -178,20 +173,27 @@ public void onStop() {
178173
}
179174

180175
public void onPause() {
181-
super.onPause();
182176
releaseCamera();
183177
preview.removeAllViews();
184178
mPreview = null;
179+
super.onPause();
185180
}
186181

187182
@Override
188183
protected void onResume() {
189184
super.onResume();
190-
/* Create a fresh instance of CameraPreview to manage the camera */
185+
/* Get an instance of the default camera */
186+
mCamera = getCameraInstance();
187+
if (mCamera == null) {
188+
Toast.makeText(getApplicationContext(), R.string.error_camera,
189+
Toast.LENGTH_LONG);
190+
this.finish();
191+
}
192+
/* We create an instance of CameraPreview to manage the camera */
191193
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
192194
/* Find the frame that will contain the camera preview */
193195
preview = (FrameLayout) findViewById(R.id.frameLayout);
194-
/* Add new view to frame */
196+
/* Add view to frame */
195197
preview.addView(mPreview);
196198
}
197199

0 commit comments

Comments
 (0)