0

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;

  • 2
    If you have to take picture from camera so why you are not using startActivityForResult() , see this link ;- https://stackoverflow.com/a/5991757/2919483 – Rajshree Tiwari Jul 26 '18 at 17:28
  • To answer your question, I did use startActivityForResult() from the MainActivity to start the Camera Activity, which this method is in – Nikhil Chatterjee Jul 28 '18 at 03:20

0 Answers0