Merge pull request #166 from christopherdro/android-temp-dir

[Android] Add option for capturing to temp directory
This commit is contained in:
Loch Wansbrough 2016-01-28 10:30:37 -08:00
commit 3df36a699c
2 changed files with 45 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# react-native-camera [![npm version](https://badge.fury.io/js/react-native-camera.svg)](http://badge.fury.io/js/react-native-camera) [![Gitter](https://badges.gitter.im/lwansbrough/react-native-camera.svg)](https://gitter.im/lwansbrough/react-native-camera)
A camera module for React Native.
A camera module for React Native.
![](https://i.imgur.com/5j2JdUk.gif)
## Known Issues
@ -154,7 +154,7 @@ The type of capture that will be performed by the camera - either a still image
#### `captureTarget`
Values: `Camera.constants.CaptureTarget.cameraRoll` (default), `Camera.constants.CaptureTarget.disk`, ~~`Camera.constants.CaptureTarget.memory`~~ (deprecated),
Values: `Camera.constants.CaptureTarget.cameraRoll` (default), `Camera.constants.CaptureTarget.disk`, `Camera.constants.CaptureTarget.temp`, ~~`Camera.constants.CaptureTarget.memory`~~ (deprecated),
This property allows you to specify the target output of the captured image data. By default the image binary is sent back as a base 64 encoded string. The disk output has been shown to improve capture response time, so that is the recommended value.

View File

@ -33,6 +33,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
public static final int RCT_CAMERA_CAPTURE_TARGET_MEMORY = 0;
public static final int RCT_CAMERA_CAPTURE_TARGET_DISK = 1;
public static final int RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL = 2;
public static final int RCT_CAMERA_CAPTURE_TARGET_TEMP = 3;
public static final int RCT_CAMERA_ORIENTATION_AUTO = 0;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_LEFT = 1;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_RIGHT = 2;
@ -109,6 +110,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
put("memory", RCT_CAMERA_CAPTURE_TARGET_MEMORY);
put("disk", RCT_CAMERA_CAPTURE_TARGET_DISK);
put("cameraRoll", RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL);
put("temp", RCT_CAMERA_CAPTURE_TARGET_TEMP);
}
});
}
@ -189,6 +191,25 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
}
callback.invoke(null, Uri.fromFile(pictureFile).toString());
break;
case RCT_CAMERA_CAPTURE_TARGET_TEMP:
File tempFile = getTempMediaFile(MEDIA_TYPE_IMAGE);
if (tempFile == null) {
callback.invoke("Error creating media file.", null);
return;
}
try {
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
callback.invoke("File not found: " + e.getMessage(), null);
} catch (IOException e) {
callback.invoke("Error accessing file: " + e.getMessage(), null);
}
callback.invoke(null, Uri.fromFile(tempFile).toString());
break;
}
}
});
@ -226,4 +247,26 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
}
return mediaFile;
}
private File getTempMediaFile(int type) {
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File outputDir = _reactContext.getCacheDir();
File outputFile;
if (type == MEDIA_TYPE_IMAGE) {
outputFile = File.createTempFile("IMG_" + timeStamp, ".jpg", outputDir);
} else if (type == MEDIA_TYPE_VIDEO) {
outputFile = File.createTempFile("VID_" + timeStamp, ".mp4", outputDir);
} else {
Log.e(TAG, "Unsupported media type:" + type);
return null;
}
return outputFile;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return null;
}
}
}