|
21 | 21 | import android.util.Log; |
22 | 22 | import android.view.Menu; |
23 | 23 | import android.widget.FrameLayout; |
| 24 | +import android.widget.Toast; |
24 | 25 |
|
25 | 26 | public class MainActivity extends Activity { |
26 | 27 |
|
@@ -52,14 +53,6 @@ protected void onCreate(Bundle savedInstanceState) { |
52 | 53 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
53 | 54 | /* Provide continuous autofocus if camera does not support it */ |
54 | 55 | 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); |
63 | 56 |
|
64 | 57 | /* Initialize ICE, copied from an example */ |
65 | 58 | /**************************************************************************/ |
@@ -104,6 +97,8 @@ private void releaseCamera() { |
104 | 97 | if (mCamera != null) { |
105 | 98 | /* Disable callbacks */ |
106 | 99 | mCamera.setPreviewCallback(null); |
| 100 | + mCamera.stopPreview(); |
| 101 | + mCamera.lock(); |
107 | 102 | mCamera.release(); |
108 | 103 | mCamera = null; |
109 | 104 | /* Save the state */ |
@@ -178,20 +173,27 @@ public void onStop() { |
178 | 173 | } |
179 | 174 |
|
180 | 175 | public void onPause() { |
181 | | - super.onPause(); |
182 | 176 | releaseCamera(); |
183 | 177 | preview.removeAllViews(); |
184 | 178 | mPreview = null; |
| 179 | + super.onPause(); |
185 | 180 | } |
186 | 181 |
|
187 | 182 | @Override |
188 | 183 | protected void onResume() { |
189 | 184 | 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 */ |
191 | 193 | mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); |
192 | 194 | /* Find the frame that will contain the camera preview */ |
193 | 195 | preview = (FrameLayout) findViewById(R.id.frameLayout); |
194 | | - /* Add new view to frame */ |
| 196 | + /* Add view to frame */ |
195 | 197 | preview.addView(mPreview); |
196 | 198 | } |
197 | 199 |
|
|
0 commit comments