Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

android - What should i do so that camera work everytime?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...