mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-04-13 06:52:05 +02:00
add a couple extra checks to prevent crashing
This commit is contained in:
parent
f158d2ffbd
commit
abb2ca51ab
@ -135,19 +135,24 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
return;
|
||||
}
|
||||
|
||||
disableFlash();
|
||||
hideTimer();
|
||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
||||
currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT;
|
||||
toggleCameraBtn.setImageResource(R.mipmap.camera_rear);
|
||||
} else {
|
||||
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||
toggleCameraBtn.setImageResource(R.mipmap.camera_front);
|
||||
}
|
||||
|
||||
disableFlash();
|
||||
int newIconId = R.mipmap.camera_front;
|
||||
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)
|
||||
@ -239,11 +244,14 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
}
|
||||
|
||||
private void initVideoButtons() {
|
||||
final Resources res = getResources();
|
||||
togglePhotoVideoBtn.setImageDrawable(res.getDrawable(R.mipmap.photo));
|
||||
shutterBtn.setImageDrawable(res.getDrawable(R.mipmap.video_rec));
|
||||
preview.initRecorder();
|
||||
toggleCameraBtn.setVisibility(View.VISIBLE);
|
||||
if (preview.initRecorder()) {
|
||||
final Resources res = getResources();
|
||||
togglePhotoVideoBtn.setImageDrawable(res.getDrawable(R.mipmap.photo));
|
||||
shutterBtn.setImageDrawable(res.getDrawable(R.mipmap.video_rec));
|
||||
toggleCameraBtn.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
Utils.showToast(getApplicationContext(), R.string.video_mode_error);
|
||||
}
|
||||
}
|
||||
|
||||
private void hideNavigationBarIcons() {
|
||||
@ -286,16 +294,19 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
toggleCameraBtn.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
preview.setCamera(currCamera);
|
||||
hideNavigationBarIcons();
|
||||
if (preview.setCamera(currCamera)) {
|
||||
hideNavigationBarIcons();
|
||||
|
||||
if (sensorManager != null) {
|
||||
final Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
if (sensorManager != null) {
|
||||
final Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
|
||||
if (!isInPhotoMode) {
|
||||
initVideoButtons();
|
||||
if (!isInPhotoMode) {
|
||||
initVideoButtons();
|
||||
}
|
||||
} else {
|
||||
Utils.showToast(getApplicationContext(), R.string.camera_switch_error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -371,4 +382,11 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (preview != null)
|
||||
preview.releaseCamera();
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
curVideoPath = "";
|
||||
}
|
||||
|
||||
public void setCamera(int cameraId) {
|
||||
public boolean setCamera(int cameraId) {
|
||||
currCameraId = cameraId;
|
||||
Camera newCamera;
|
||||
try {
|
||||
@ -82,11 +82,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
Utils.showToast(getContext(), R.string.camera_open_error);
|
||||
Log.e(TAG, "setCamera open " + e.getMessage());
|
||||
callback.setIsCameraAvailable(false);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (camera == newCamera) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
releaseCamera();
|
||||
@ -109,6 +109,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
camera.setPreviewDisplay(surfaceHolder);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
setupPreview();
|
||||
}
|
||||
@ -119,6 +120,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
if (isVideoMode) {
|
||||
initRecorder();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setTargetUri(Uri uri) {
|
||||
@ -455,12 +458,14 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
}
|
||||
|
||||
// VIDEO RECORDING
|
||||
public void initRecorder() {
|
||||
public boolean initRecorder() {
|
||||
if (camera == null || recorder != null || !isSurfaceCreated)
|
||||
return;
|
||||
return false;
|
||||
|
||||
camera.lock();
|
||||
final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo();
|
||||
if (preferred == null)
|
||||
return false;
|
||||
|
||||
parameters.setPreviewSize(preferred.width, preferred.height);
|
||||
camera.setParameters(parameters);
|
||||
|
||||
@ -474,7 +479,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
curVideoPath = Utils.getOutputMediaFile(getContext(), false);
|
||||
if (curVideoPath.isEmpty()) {
|
||||
Utils.showToast(getContext(), R.string.video_creating_error);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
Log.e(TAG, "initRecorder " + e.getMessage());
|
||||
releaseCamera();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean toggleRecording() {
|
||||
|
@ -5,8 +5,10 @@
|
||||
<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_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="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_audio_permissions">We need the audio permission for recording videos</string>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user