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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…