mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-06-27 09:02:59 +02:00
adding a couple safety checks for camera access and video saving
This commit is contained in:
@ -48,6 +48,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
private boolean isFlashEnabled;
|
||||
private boolean isPhoto;
|
||||
private boolean isAskingPermissions;
|
||||
private boolean isCameraAvailable;
|
||||
private int currVideoRecTimer;
|
||||
private Handler timerHandler;
|
||||
|
||||
@ -114,6 +115,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
|
||||
@OnClick(R.id.toggle_camera)
|
||||
public void toggleCamera() {
|
||||
if (!checkCameraAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
disableFlash();
|
||||
hideTimer();
|
||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
||||
@ -131,6 +136,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
|
||||
@OnClick(R.id.toggle_flash)
|
||||
public void toggleFlash() {
|
||||
if (!checkCameraAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isFlashEnabled) {
|
||||
disableFlash();
|
||||
} else {
|
||||
@ -152,6 +161,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
|
||||
@OnClick(R.id.shutter)
|
||||
public void shutterPressed() {
|
||||
if (!checkCameraAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isPhoto) {
|
||||
preview.takePicture();
|
||||
} else {
|
||||
@ -177,6 +190,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
|
||||
@OnClick(R.id.toggle_videocam)
|
||||
public void toggleVideo() {
|
||||
if (!checkCameraAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Utils.hasAudioPermission(getApplicationContext())) {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, AUDIO_PERMISSION);
|
||||
isAskingPermissions = true;
|
||||
@ -295,6 +312,13 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
|
||||
}
|
||||
|
||||
private boolean checkCameraAvailable() {
|
||||
if (!isCameraAvailable) {
|
||||
Utils.showToast(getApplicationContext(), R.string.camera_unavailable);
|
||||
}
|
||||
return isCameraAvailable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFlashAvailable(boolean available) {
|
||||
if (available) {
|
||||
@ -304,4 +328,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||
disableFlash();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIsCameraAvailable(boolean available) {
|
||||
isCameraAvailable = available;
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -64,9 +65,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
Camera newCamera;
|
||||
try {
|
||||
newCamera = Camera.open(cameraId);
|
||||
callback.setIsCameraAvailable(true);
|
||||
} catch (Exception e) {
|
||||
Utils.showToast(getContext(), R.string.camera_open_error);
|
||||
Log.e(TAG, "setCamera open " + e.getMessage());
|
||||
callback.setIsCameraAvailable(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -351,6 +354,9 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
|
||||
// VIDEO RECORDING
|
||||
public void initRecorder() {
|
||||
if (camera == null)
|
||||
return;
|
||||
|
||||
isRecording = false;
|
||||
isVideoMode = true;
|
||||
recorder = new MediaRecorder();
|
||||
@ -399,9 +405,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
|
||||
private void stopRecording() {
|
||||
if (recorder != null && isRecording) {
|
||||
try {
|
||||
recorder.stop();
|
||||
recorder = null;
|
||||
Utils.scanFile(curVideoPath, getContext());
|
||||
} catch (RuntimeException e) {
|
||||
new File(curVideoPath).delete();
|
||||
Utils.showToast(getContext(), R.string.video_saving_error);
|
||||
} finally {
|
||||
recorder = null;
|
||||
}
|
||||
}
|
||||
|
||||
isRecording = false;
|
||||
@ -409,5 +421,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||
|
||||
public interface PreviewListener {
|
||||
void setFlashAvailable(boolean available);
|
||||
|
||||
void setIsCameraAvailable(boolean available);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
<resources>
|
||||
<string name="app_name">Simple Camera</string>
|
||||
<string name="camera_open_error">An error occurred at obtaining the camera</string>
|
||||
<string name="camera_unavailable">Camera unavailable</string>
|
||||
<string name="camera_open_error">An error occurred at accessing the camera</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_directory">Simple Videos</string>
|
||||
<string name="photo_directory">Simple Photos</string>
|
||||
<string name="no_permissions">Not much to do without accessing your camera and storage</string>
|
||||
|
Reference in New Issue
Block a user