Reputation: 126
I am working on an Android application which programatically capture image using both front and back camera and save to a folder in the internal memory. Once the image is saved the application will send the contents of the folder through email. How to wait the application till images captured completely, otherwise a blank email is send?
class MainActivity{
//other codes
public void buttonClick(View v) {
CameraService.startCamera(0, true);
sendEmail();
}
public void sendEmail()
{
//get contents from the folder and send the contents using java mail api
}
}
This is the class which capture image using both front and back camera.
class CamearService
{
public static void startCamera(int cameraID,final boolean isFirstTime) {
mCamera = Camera.open(cameraID);
try {
mCamera.setPreviewTexture(new SurfaceTexture(10));
} catch (IOException e1) {
}
Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
params.setPictureFormat(ImageFormat.JPEG);
params.setJpegQuality(100);
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.takePicture(null, null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i("hello", "picture-taken");
if (data != null) {
mCamera.stopPreview();
mCamera.release();
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length, opts);
storeImage(bitmap); //function to store image to local folder
if(isFirstTime)
{
//Capture using front camera
CameraService.startCamera(1, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
when i click on the button startCamera() function is executed and before capturing the second image sendEmail() function is executed. As a result only one image is send through email.
Upvotes: 1
Views: 1314
Reputation: 10267
What you are looking for is basic programming concept called Callbacks (which you are using with the Camera.takePicture() method call).
Please read my answer here as it is apply as well to your case: Handle data returned by an Async task (Firebase)
Upvotes: 1