finish()方法未完成活动

时间:2018-07-26 17:05:58

标签: java android android-camera2 activity-finish

我想在拍照后回到主要活动。

但是,finish()方法并没有带我回到Main Activity。这是我的代码。

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);
}

如果我运行此代码,则结果图片不为null,但活动未完成。如果我用     while(图片== null); 结果图片为空,但活动在方法末尾结束。我想让图片不为空,并让Activity完成。任何帮助将不胜感激;

0 个答案:

没有答案