From 12afd8443ac3bb0aaaca17f2a11c349d81e465d1 Mon Sep 17 00:00:00 2001 From: Rob Stemen Date: Sun, 22 Jan 2017 21:33:17 -0600 Subject: [PATCH] Fixed issue where Android camera would crash the application if the user attempted to capture too many pictures in a short timeframe. (#553) --- .../lwansbrough/RCTCamera/RCTCameraModule.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java index 4f4dd95..2782eab 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -87,6 +87,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule private Camera mCamera = null; private Promise mRecordingPromise = null; private ReadableMap mRecordingOptions; + private Boolean mSafeToCapture = true; public RCTCameraModule(ReactApplicationContext reactContext) { super(reactContext); @@ -657,7 +658,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation); camera.setPreviewCallback(null); - camera.takePicture(null, null, new Camera.PictureCallback() { + + Camera.PictureCallback captureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { @@ -734,8 +736,19 @@ public class RCTCameraModule extends ReactContextBaseJavaModule break; } } + + mSafeToCapture = true; } - }); + }; + + if(mSafeToCapture) { + try { + camera.takePicture(null, null, captureCallback); + mSafeToCapture = false; + } catch(RuntimeException ex) { + Log.e(TAG, "Couldn't capture photo.", ex); + } + } } @ReactMethod