I am trying to come back to the Main Activity after taking a picture.
However, the finish() method is not taking me back to the Main Activity. Here is my code.
protected void takePicture() {
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
picture = bytes;
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (image != null) {
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(file);
output.write(bytes);
} finally {
if (null != output) {
output.close();
}
}
}
};
} catch (CameraAccessException e) {
e.printStackTrace();
}
while(picture == null);
sendImage(picture);
finish();
}
private void sendImage(byte[] bytes) {
Intent i = new Intent();
i.putExtra("picture", bytes);
setResult(1, i);
}
If I run this code, the result picture is not null, but the Activity does not finish. If I comment out the line with while(picture == null); the resulting picture is null but the activity finishes at the end of the method. I want to have both the picture not be null and the Activity to finish. Any help would be greatly appreciated;