Android camera2: java.lang.IllegalStateException: maxImages (1) has already been acquired, call #close before acquiring more

依然范特西╮ 提交于 2020-07-04 10:48:30

问题


Hello having trouble to fix this issue.

I already have a imageReader.close called inside the ImageAvailable callback but still having the error:

java.lang.IllegalStateException: maxImages (1) has already been acquired, call #close before acquiring more.

Code I have is here:

private ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener()
{
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image img = mReader.acquireLatestImage();

        mReader.close();
    }

};

ps. I also use the argument reader as well but not seem to solve the problem


回答1:


Ok I have solved my problem. I need to close the img object not the ImageReader.




回答2:


    private ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {
        String status = Environment.getExternalStorageState();
        if (!status.equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getApplicationContext(), "your SD card is not available", Toast.LENGTH_SHORT).show();
            return;
        }
        Image image = reader.acquireNextImage();
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        byte[] data = new byte[buffer.remaining()];
        buffer.get(data);
        image.close();//after you use the image's content ,you can close it
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
        String picturePath = System.currentTimeMillis() + ".jpg";
        imgFile = new File(filePath, picturePath);
        Uri uri = Uri.fromFile(imgFile);
        try {//Store to folder
            FileOutputStream fileOutputStream = new FileOutputStream(imgFile);
            fileOutputStream.write(data);
            fileOutputStream.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
        startEditPictureActivity(uri, imgFile);


    }
};


来源:https://stackoverflow.com/questions/32535739/android-camera2-java-lang-illegalstateexception-maximages-1-has-already-been

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!