Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using camera service in my application. Sometimes the camera service is running fine in the application and sometimes it gives a runtime exception.

I have put Camera.Open() in try block and i have catched the exception and its showing in log cat

03-12 13:52:42.211: D/crazy(12686): in catch1
03-12 13:52:42.211: D/crazy(12686): java.lang.RuntimeException: Fail to connect to camera service

The code that i done is...

    TelephonyManager mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            int callState = mgr.getCallState();

            //state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(callState==TelephonyManager.CALL_STATE_RINGING) {
            try {


                cam = Camera.open();
                p = cam.getParameters();

                String myString = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";
                long blinkDelay = 50;


                for (int i = 0; i < myString.length(); i++) {
                    //state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                    callState = mgr.getCallState();
                    if (callState==TelephonyManager.CALL_STATE_IDLE){
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        break;                  

                        }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        break;  
                        }               

                    if (myString.charAt(i) == '0') {
                        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(p);
                    } else {
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        cam.setParameters(p);
                    }

                        Thread.sleep(blinkDelay);

                }
            }catch (Exception e) {
                // TODO: handle exception
                Log.d(tag, "in catch1");
                Log.d(tag, e.toString());

        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
241 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's probably because it is already used.

The javadoc for open states :

If the same camera is opened by other applications, this will throw a RuntimeException.

You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.

Your application should only have one Camera object active at a time for a particular hardware camera.

Make sure you always release the camera (even in case of exception, use finally) and check if there is no other application using it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...