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 isFlashEnabled;
|
||||||
private boolean isPhoto;
|
private boolean isPhoto;
|
||||||
private boolean isAskingPermissions;
|
private boolean isAskingPermissions;
|
||||||
|
private boolean isCameraAvailable;
|
||||||
private int currVideoRecTimer;
|
private int currVideoRecTimer;
|
||||||
private Handler timerHandler;
|
private Handler timerHandler;
|
||||||
|
|
||||||
@ -114,6 +115,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
|
|
||||||
@OnClick(R.id.toggle_camera)
|
@OnClick(R.id.toggle_camera)
|
||||||
public void toggleCamera() {
|
public void toggleCamera() {
|
||||||
|
if (!checkCameraAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
disableFlash();
|
disableFlash();
|
||||||
hideTimer();
|
hideTimer();
|
||||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
||||||
@ -131,6 +136,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
|
|
||||||
@OnClick(R.id.toggle_flash)
|
@OnClick(R.id.toggle_flash)
|
||||||
public void toggleFlash() {
|
public void toggleFlash() {
|
||||||
|
if (!checkCameraAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isFlashEnabled) {
|
if (isFlashEnabled) {
|
||||||
disableFlash();
|
disableFlash();
|
||||||
} else {
|
} else {
|
||||||
@ -152,6 +161,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
|
|
||||||
@OnClick(R.id.shutter)
|
@OnClick(R.id.shutter)
|
||||||
public void shutterPressed() {
|
public void shutterPressed() {
|
||||||
|
if (!checkCameraAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isPhoto) {
|
if (isPhoto) {
|
||||||
preview.takePicture();
|
preview.takePicture();
|
||||||
} else {
|
} else {
|
||||||
@ -177,6 +190,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
|
|
||||||
@OnClick(R.id.toggle_videocam)
|
@OnClick(R.id.toggle_videocam)
|
||||||
public void toggleVideo() {
|
public void toggleVideo() {
|
||||||
|
if (!checkCameraAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!Utils.hasAudioPermission(getApplicationContext())) {
|
if (!Utils.hasAudioPermission(getApplicationContext())) {
|
||||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, AUDIO_PERMISSION);
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, AUDIO_PERMISSION);
|
||||||
isAskingPermissions = true;
|
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
|
@Override
|
||||||
public void setFlashAvailable(boolean available) {
|
public void setFlashAvailable(boolean available) {
|
||||||
if (available) {
|
if (available) {
|
||||||
@ -304,4 +328,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
disableFlash();
|
disableFlash();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setIsCameraAvailable(boolean available) {
|
||||||
|
isCameraAvailable = available;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import android.view.SurfaceView;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -64,9 +65,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
Camera newCamera;
|
Camera newCamera;
|
||||||
try {
|
try {
|
||||||
newCamera = Camera.open(cameraId);
|
newCamera = Camera.open(cameraId);
|
||||||
|
callback.setIsCameraAvailable(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
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);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,6 +354,9 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
// VIDEO RECORDING
|
// VIDEO RECORDING
|
||||||
public void initRecorder() {
|
public void initRecorder() {
|
||||||
|
if (camera == null)
|
||||||
|
return;
|
||||||
|
|
||||||
isRecording = false;
|
isRecording = false;
|
||||||
isVideoMode = true;
|
isVideoMode = true;
|
||||||
recorder = new MediaRecorder();
|
recorder = new MediaRecorder();
|
||||||
@ -399,9 +405,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
private void stopRecording() {
|
private void stopRecording() {
|
||||||
if (recorder != null && isRecording) {
|
if (recorder != null && isRecording) {
|
||||||
recorder.stop();
|
try {
|
||||||
recorder = null;
|
recorder.stop();
|
||||||
Utils.scanFile(curVideoPath, getContext());
|
Utils.scanFile(curVideoPath, getContext());
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
new File(curVideoPath).delete();
|
||||||
|
Utils.showToast(getContext(), R.string.video_saving_error);
|
||||||
|
} finally {
|
||||||
|
recorder = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isRecording = false;
|
isRecording = false;
|
||||||
@ -409,5 +421,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
public interface PreviewListener {
|
public interface PreviewListener {
|
||||||
void setFlashAvailable(boolean available);
|
void setFlashAvailable(boolean available);
|
||||||
|
|
||||||
|
void setIsCameraAvailable(boolean available);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Simple Camera</string>
|
<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_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="video_directory">Simple Videos</string>
|
||||||
<string name="photo_directory">Simple Photos</string>
|
<string name="photo_directory">Simple Photos</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>
|
||||||
|
Reference in New Issue
Block a user