How to release camera after activity ends in Android?

后端 未结 1 1801
孤街浪徒
孤街浪徒 2020-12-09 23:12

I am working on an application that requires to scan a QR code and click pictures, but sometimes it so happens that the camera application crashes and it says that the Andro

相关标签:
1条回答
  • 2020-12-09 23:34

    Put Below code in your onDestroy method of your activity:

    protected void onDestroy(){
    
    if(camera!=null){
                camera.stopPreview();
                camera.setPreviewCallback(null);
    
                camera.release();
                camera = null;
            }
    
    
    }
    

    If you are using separate Preview class then add below code in that:

    public void surfaceDestroyed(SurfaceHolder holder) {
    
            if(camera!=null){
                camera.stopPreview();
                camera.setPreviewCallback(null);
    
                camera.release();
                camera = null;
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题