首页 > 解决方案 > finish() 方法未完成活动

问题描述

拍照后我正试图回到主要活动。

然而,finish() 方法并没有把我带回主活动。这是我的代码。

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

如果我运行这段代码,结果图片不为空,但 Activity 没有完成。如果我用 while(picture == null); 注释掉该行 结果图片为空,但活动在方法结束时完成。我希望图片不为空,并且要完成活动。任何帮助将不胜感激;

标签: javaandroidandroid-camera2activity-finish

解决方案


推荐阅读