add a couple extra checks to prevent crashing

This commit is contained in:
tibbi 2016-06-17 14:51:32 +02:00
parent f158d2ffbd
commit abb2ca51ab
3 changed files with 53 additions and 26 deletions

View File

@ -135,19 +135,24 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
return; return;
} }
disableFlash();
hideTimer();
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) { if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT; currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT;
toggleCameraBtn.setImageResource(R.mipmap.camera_rear);
} else { } else {
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK; currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
toggleCameraBtn.setImageResource(R.mipmap.camera_front);
} }
disableFlash(); int newIconId = R.mipmap.camera_front;
preview.releaseCamera(); preview.releaseCamera();
preview.setCamera(currCamera); if (preview.setCamera(currCamera)) {
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
newIconId = R.mipmap.camera_rear;
}
toggleCameraBtn.setImageResource(newIconId);
disableFlash();
hideTimer();
} else {
Utils.showToast(getApplicationContext(), R.string.camera_switch_error);
}
} }
@OnClick(R.id.toggle_flash) @OnClick(R.id.toggle_flash)
@ -239,11 +244,14 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
} }
private void initVideoButtons() { private void initVideoButtons() {
if (preview.initRecorder()) {
final Resources res = getResources(); final Resources res = getResources();
togglePhotoVideoBtn.setImageDrawable(res.getDrawable(R.mipmap.photo)); togglePhotoVideoBtn.setImageDrawable(res.getDrawable(R.mipmap.photo));
shutterBtn.setImageDrawable(res.getDrawable(R.mipmap.video_rec)); shutterBtn.setImageDrawable(res.getDrawable(R.mipmap.video_rec));
preview.initRecorder();
toggleCameraBtn.setVisibility(View.VISIBLE); toggleCameraBtn.setVisibility(View.VISIBLE);
} else {
Utils.showToast(getApplicationContext(), R.string.video_mode_error);
}
} }
private void hideNavigationBarIcons() { private void hideNavigationBarIcons() {
@ -286,7 +294,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
toggleCameraBtn.setVisibility(View.INVISIBLE); toggleCameraBtn.setVisibility(View.INVISIBLE);
} }
preview.setCamera(currCamera); if (preview.setCamera(currCamera)) {
hideNavigationBarIcons(); hideNavigationBarIcons();
if (sensorManager != null) { if (sensorManager != null) {
@ -297,6 +305,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
if (!isInPhotoMode) { if (!isInPhotoMode) {
initVideoButtons(); initVideoButtons();
} }
} else {
Utils.showToast(getApplicationContext(), R.string.camera_switch_error);
}
} }
@Override @Override
@ -371,4 +382,11 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
finish(); finish();
} }
} }
@Override
protected void onDestroy() {
super.onDestroy();
if (preview != null)
preview.releaseCamera();
}
} }

View File

@ -72,7 +72,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
curVideoPath = ""; curVideoPath = "";
} }
public void setCamera(int cameraId) { public boolean setCamera(int cameraId) {
currCameraId = cameraId; currCameraId = cameraId;
Camera newCamera; Camera newCamera;
try { try {
@ -82,11 +82,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
Utils.showToast(getContext(), R.string.camera_open_error); Utils.showToast(getContext(), R.string.camera_open_error);
Log.e(TAG, "setCamera open " + e.getMessage()); Log.e(TAG, "setCamera open " + e.getMessage());
callback.setIsCameraAvailable(false); callback.setIsCameraAvailable(false);
return; return false;
} }
if (camera == newCamera) { if (camera == newCamera) {
return; return false;
} }
releaseCamera(); releaseCamera();
@ -109,6 +109,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
camera.setPreviewDisplay(surfaceHolder); camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage()); Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage());
return false;
} }
setupPreview(); setupPreview();
} }
@ -119,6 +120,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
if (isVideoMode) { if (isVideoMode) {
initRecorder(); initRecorder();
} }
return true;
} }
public void setTargetUri(Uri uri) { public void setTargetUri(Uri uri) {
@ -455,12 +458,14 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} }
// VIDEO RECORDING // VIDEO RECORDING
public void initRecorder() { public boolean initRecorder() {
if (camera == null || recorder != null || !isSurfaceCreated) if (camera == null || recorder != null || !isSurfaceCreated)
return; return false;
camera.lock();
final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo(); final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo();
if (preferred == null)
return false;
parameters.setPreviewSize(preferred.width, preferred.height); parameters.setPreviewSize(preferred.width, preferred.height);
camera.setParameters(parameters); camera.setParameters(parameters);
@ -474,7 +479,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
curVideoPath = Utils.getOutputMediaFile(getContext(), false); curVideoPath = Utils.getOutputMediaFile(getContext(), false);
if (curVideoPath.isEmpty()) { if (curVideoPath.isEmpty()) {
Utils.showToast(getContext(), R.string.video_creating_error); Utils.showToast(getContext(), R.string.video_creating_error);
return; return false;
} }
final Camera.Size videoSize = getOptimalVideoSize(); final Camera.Size videoSize = getOptimalVideoSize();
@ -495,7 +500,9 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
Utils.showToast(getContext(), R.string.video_setup_error); Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "initRecorder " + e.getMessage()); Log.e(TAG, "initRecorder " + e.getMessage());
releaseCamera(); releaseCamera();
return false;
} }
return true;
} }
public boolean toggleRecording() { public boolean toggleRecording() {

View File

@ -5,8 +5,10 @@
<string name="video_creating_error">An error occurred at creating the video file</string> <string name="video_creating_error">An error occurred at creating the video file</string>
<string name="video_saving_error">An error occurred at saving the video file</string> <string name="video_saving_error">An error occurred at saving the video file</string>
<string name="video_setup_error">An error occurred at setting up the recorder</string> <string name="video_setup_error">An error occurred at setting up the recorder</string>
<string name="video_mode_error">Switching to video mode failed</string>
<string name="video_directory">Simple Videos</string> <string name="video_directory">Simple Videos</string>
<string name="photo_directory">Simple Photos</string> <string name="photo_directory">Simple Photos</string>
<string name="camera_switch_error">Switching camera failed</string>
<string name="no_permissions">Not much to do without accessing your camera and storage</string> <string name="no_permissions">Not much to do without accessing your camera and storage</string>
<string name="no_audio_permissions">We need the audio permission for recording videos</string> <string name="no_audio_permissions">We need the audio permission for recording videos</string>