Skip to content

Instantly share code, notes, and snippets.

@MichaelObi
Last active April 23, 2017 19:05
Show Gist options
  • Save MichaelObi/58fc52ecb44c8d9c212dab9bd2233ec2 to your computer and use it in GitHub Desktop.
Save MichaelObi/58fc52ecb44c8d9c212dab9bd2233ec2 to your computer and use it in GitHub Desktop.
Save photo from a Camera source to a file
public void takePhoto() {
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes) {
try {
File basePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "vstyle");
if (!basePath.exists()) {
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
}
File captureFile = new File(basePath + "photo_" + (new Date().getTime()) + ".jpg");
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment