app hanging on camera.release()
Asked Answered
E

4

3

I'm working on a flashlight app using the camera flash. It seems to work fine but on occasion calling camera.release() causes a hang for about a minute or so. I've included the code below. I've looked at a bunch of examples and I don't see anything that could cause such a thing. Any ideas?

    //latest
    public void setOn(boolean on, Context context) {    

    if (lock) {
        Log.i(TAG, "lock: true");
        return;
    }


    if (on) {           
        if (mCamera == null) {
            mCamera = Camera.open();
        }

        Parameters params = mCamera.getParameters();
        params.setFlashMode(MODE_TORCH);
        mCamera.setParameters(params);
        mCamera.startPreview();
    } else {
        if (mCamera != null) {              
            try {                   
                Parameters params = mCamera.getParameters();
                params.setFlashMode(MODE_OFF);
                mCamera.setParameters(params);                  
            } finally {
                new Thread(new Runnable() {
                    public void run() {
                        Log.i(TAG, "new Thread - start");
                        lock = true;
                        mCamera.setPreviewCallback(null);
                        mCamera.stopPreview();
                        mCamera.release();
                        mCamera = null;
                        lock = false;
                        Log.i(TAG, "new Thread - end");
                    }
                }).start();                                     
            }
        }
    }
}


//original
public void setOn(boolean on, Context context) {        
    Camera camera = mCamera;
    if (on) {           
        if (camera == null) {
            mCamera = camera = Camera.open();
        }

        Parameters params = camera.getParameters();
        params.setFlashMode(MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
    } else {
        if (camera != null) {

            try {                   
                Parameters params = camera.getParameters();
                params.setFlashMode(MODE_OFF);
                camera.setParameters(params);                   
            } finally {
                camera.stopPreview();
                camera.release();
                mCamera = camera = null;
            }
        }
    }
}
Eleonoreleonora answered 9/1, 2012 at 1:41 Comment(1)
On which device does this happen? We have the exact same problem on Nexus 10 with Androids 4.3, 4.4 and 5.0 (4.2 did not have that problem)Breastplate
A
1

Try putting it in a thread to run in the background so it wont hang up the UI.

new Thread(new Runnable(){
    public void run(){
        camera.setPreviewCallback(null); // PreviewCallback de_init.
        camera.stopPreview(); // stop Preview
        camera.release();
    }
}).start();
Arciniega answered 9/1, 2012 at 2:3 Comment(4)
Thanks; the UI is no longer hanging. camera.release() however is still hanging in this new thread. Any clue as to why?Eleonoreleonora
Sorry, forgot to attach it =) code.google.com/p/android/issues/detail?id=1578Arciniega
I came across that thread earlier today. It sheds a bit of light into why it may be hanging but offer no solution since it claims that a bug fix went in to address this almost 2 years ago.Eleonoreleonora
Do you have any other ideas on what the problem might be? I noticed that other examples do not use start/stop preview. When I remove these it no longer hangs but the camera flash does not light up. android-er.blogspot.com/2011/02/…Eleonoreleonora
T
0

For me, the solution that works is:

Try{
    camera.stopPreview();
    camera.setPreviewCallback(null);   
    camera.release();
    camera = null;
} catch (Exception e){
    //...
}
Tamalatamale answered 16/7, 2012 at 3:7 Comment(0)
A
0

I`ve solved this isuue adding camera.unlock() before release()

camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;

test on more devices needed...

Atory answered 31/7, 2013 at 15:48 Comment(1)
Unfortunately, this does not solve the issue we are having on Nexus 10.Breastplate
F
0

just call your methods like

mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();

you have to call setPreviewCallback(null) betweeen stopPreview and camera.releass

Flatter answered 6/5, 2015 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.