2

I'm writing an android app. I encounter the following problem:

app passed NULL surface

while executing the following code:



    public void takePictureNoPreview(Context context){
            try {
                myCamera = Camera.open(0);
            } catch (Exception e) {
                e.printStackTrace();
                console.append("Failed to connect to camera\n");
            }
            if(myCamera!=null){
                SurfaceView dummy=new SurfaceView(context);
                try {
                    myCamera.setPreviewDisplay(dummy.getHolder());
                    myCamera.startPreview(); 
                    myCamera.takePicture(null, null, getJpegCallback());
                } 
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }    
                finally
                {
                    myCamera.stopPreview();
                    myCamera.release();
                }
            }  


The main goal is to take a picture without the surfaceview, in order to store it and send via email as quickly as possible.

Thanks in advance.

2 Answers2

3

I think you should implement the interface SurfaceHolder.Callback first, and then add callback to the holder first, e.g.

mHolder.addCallback(callback)
myCamera.setPreviewDisplay(mHolder);
myCamera.startPreview(); 

I think it will be ok!!

If you have any problems, you can refer to 1.my https://github.com/RyanLiuNtust/Lecture-of-Android/tree/camera_lecture

RyanLiu
  • 1,557
  • 2
  • 18
  • 27
0

You can't do it. There are some privacy issues with the camera. I have seen this answer on one of the boards here before but I don't have time to look that answer up again. You can't do it, because if your app takes a picture of something they should be able to know what it is of.

W.R.T.Bees
  • 11
  • 1
  • 2
  • 1
    I take that back. I was having a problem where I could not get the preview to show up even though I had control of the camera. I could take pictures without the showing it. I extended ViewGroup and implemented SurfaceHolder.Callback. The strange thing was it worked at one point but further into development it stopped. I dont know why it stopped working, but the short answer is you should'nt have to have a surface view. It should still take a picture even when app passed null surface error shows. – W.R.T.Bees Jun 24 '13 at 19:47
  • It still seems impossible (Android 4.3). Pictures cannot be taken without a visible SurfaceView no matter what. I wasn't able to find any official statement about privacy from Google. This seems like a very annoying limitation. I've tried `camera.setPreviewDisplay(null)`, to make a dummy `SurfaceHolder`, not setting a PreviewDisplay at all but nothing worked. – Solenoid Oct 29 '13 at 23:15
  • There is a very simple solution. You can just overlap the SurfaceHolder with another View so it's not visible. – AC-OpenSource May 08 '15 at 11:45