4

I have following android code (written here in pseudocode):

mCamera = configAndInitialize(); //all I want to do before taking picture
mCamera.startPreview();
mCamera.torchOn(); //setting parameters with flash mode torch

onClick(){
    mCamera.stopPreview();
    mCamera.takePicture();
    mCamera.torchOff();
}

Sometimes (often when phone was recently restarted and camera wasn't in use until this app) this code ends with error 100 Camera server died. If camera took successfully picture before it usually works.

I was debugging it for great amount of time and I found out it works when I comment out lines with torch. I can see torch working in both cases when taking pictures works or not.

Code of torchOn is following:

if(mCamera != null){
    mCamera.stopPreview();
    Camera.Parameters p = mCamera.getParameters();
    List<String> supported = p.getSupportedFlashModes();
    if (supported.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
      p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    }
    mCamera.setParameters(p);
    mCamera.startPreview();
}

Is there any reason why taking picture could not work because of torch? I observed it happend on Motorola Razr and Samsung Galaxy SIII.

I installed on my device two version of this app (with different name and so on). And I do following:

  1. Restart device
  2. Tried app-with-torch
  3. If app-with-torch does work back in point 1.
  4. Tried app-without-torch
  5. Tried app-with-torch

And the results are followings:

  1. App-without-torch works always
  2. App-with-torch in about 80% of tries doesn't work at point 2. (after restart)
  3. App-with-torch works always at point 5. (after app-without-torch was used)

My app start working even if I add torchOff() just before taking picture.

Ari
  • 3,101
  • 2
  • 27
  • 49
  • Just curious - is there a technical reason why just launching the camera as an intent is not a possible way to get the pic you need ? (gotcha: some Samsung devices can pass data=null in activityOnResult parm.) – Howard Pautz Aug 28 '13 at 03:35
  • @HowardPautz I have customized layout and I need torch to be on, when I'm taking picture. Taking picture is an easy part. – Ari Aug 28 '13 at 07:12

2 Answers2

2

We could call this the " Monty Python Dead Parrot Log.d answer " :-P I wish I had a solution for you - do have some suggestions though. Heisenbugs are difficult to catch.

Does the torch have an isOn() test ?
Similarly (I don't recall), does the camera have an isReady() test ?

Can you tell from the logs if the camera dies before, during, or after mCamera.torchOn() or .torchOff() ?

What would happen if you stretch out the time span between calls ? This wouldn't be useable for the real app, but might help you monitor and catch what's happening. Say something like this in pseudo code:

  try {
    // Log.d ("cam", "here 1") ;
    mCamera = configAndInitialize();
    // Log.d ("cam", "here 2");
    if ( mCamera.isReady() ) {  // or isConfigured / initialized 
       // Log.d ("cam", "here 2");
       Camera.startPreview();
       // Log.d ("cam", "here 2");
       thisThread.setDelay (200); // millisecs. try even up to 2000 ms !
       // Log.d ("cam", "here 4");
       mCamera.torchOn();
       // Log.d ("cam", "here 5");
       thisThread.setDelay (200); // again up to 2000 ms
       // Log.d ("cam", "here 6");
    }
  } catch (Exception e) {
    Log.d ("oops!", e.toString() );   
  }

The other thing to monitor, or make sure of, is that the camera and torch really are ready before that onClick can fire. Sprinkle some delays like the Log.d's all around and see. It might need a call back (mCamera.isReady() then enable onClick ).

Another thing to do is see if you can dig up the camera's source code (or the torch's) and GREP for error 100 - or is that a generic android 100 ?

I'm sure you're well aware of how much stuff happens when that camera gets fired up - seems like hundreds of calls. Since some of these low level items are async (cam is hardware after all), I suspect you're getting a NPE or a insufficiently initialized object. Not all NPE's etc get trapped, so it might just die on one that would not be there if delayed or syncronized sequences are used.

( HTH - I feel your pain, ari, I had to do a lot of camera stuff recently. Debugging on Samsung SIII is prohibitively time consuming. )

[EDIT] you've probably already found this link, but just in case:

How to turn on camera flash light programmatically in Android?

Community
  • 1
  • 1
Howard Pautz
  • 415
  • 7
  • 21
  • Thanks for answer. I can switch torch on and off and so on and it works. I can see torch on and off. I can wait few minuts, after everything is ready (I mean after I can see preview and torch is on). To that point everything works fine. Then I click on screen and take picture is executed. Instead of getting callback with picture I'm getting OnError with error 100 Media server died. So I think everything is ready. It could be some bug in android or in hardware. – Ari Aug 28 '13 at 20:51
  • @Ari u r welcome. Normally better to test w/real device, of course, but what behavior do you see w/ a clean-slated emulator (following steps 1-5) ? Also, please edit into your OP the callback code for review. From comment immediately above, I gather it crashes before callback is handled ? Bug in hardware seems less likely as u r using 2 different vendor's devices. (Unless Razor and SIII use same Cam and driver code :)) Bug in android would certainly not be unheard of :-| ... probably should post your log file output too. This one's a weird one. – Howard Pautz Aug 29 '13 at 00:48
  • It doesn't crash. Instead of callback on picture I'm getting onError. – Ari Aug 30 '13 at 07:10
  • ok - please edit-post the code surrounding the callback and the area around the onError. Also the log files. Need more info to understand/replicate. Is doing startPreview() twice necessary ? – Howard Pautz Aug 30 '13 at 15:06
  • @Ari Ari, I'd like to help, but really need to see more code. – Howard Pautz Sep 03 '13 at 21:26
  • But there is no other code relative to this issue. I can do whatever I want before taking picture (I spend hours on this and I tried many different scenarios). Summary is: everything is working before taking picture. When `mCamera.takePicture` is called I cannot hear shutter sound and `mCamera.onError` is called (`Media server died error 100`) instead of jpeg `PictureCallback`. – Ari Sep 04 '13 at 09:42
  • @Ari "no other code relative to this issue" ... but you don't show the PictureCallBack code, camera.open / configAndInitialize(), and the try-catch blocks around these ? Here is a related link where the last comment at this time shows a 100 error: http://stackoverflow.com/questions/10913682/how-to-capture-and-save-an-image-using-custom-camera-in-android – Howard Pautz Sep 04 '13 at 16:29
  • I don't show `PictureCallback` because it is never called. Camera open is nearly copy-paste from android.developer.com and config was written in many versions, the simples doesn't set anything more than sample from android.developer.com. – Ari Sep 05 '13 at 06:48
1

I think this is related to each OEM's implementation of the Android camera HAL (Hardware Abstraction Layer). This problem has also happened to me, I'm not sure but I suspect most camera HALs' torch mode works only in video capture, as that's where it's most used anyways. Try recording a video with the torch on to check it out.

One possible workaround would be to set your camera's flash mode to FLASH_MODE_ON just before taking the picture and then returning to FLASH_MODE_TORCH after the pic is taken if you need it again.

npace
  • 4,218
  • 1
  • 25
  • 35
  • I'm using torch to increase photo quality, so I don't think using flash_mode_on will help me. Thanks for answer - now I know somebody else has this problem too. – Ari Sep 02 '13 at 08:45
  • 1
    @npace - I think you're probably right about this. We've had numerous weird problems with Samsung cameras for example. (And, CommonsWare, if you read this, NO, I'm not going to document that field of land mines :-P ... I don't have any legs or arms left. Coding with my nose is too time consuming as it is ...) – Howard Pautz Sep 03 '13 at 19:19
  • You should try coding a camera app that also works on LG devices, they are the furthest away from the default camera HAL in my experience... Samsung is almost like stock in comparison. – npace Sep 04 '13 at 07:25
  • @npace *ugh!* have you had problems with LG's and simple camera apps (using intent launch and onAct.Result) ? – Howard Pautz Sep 04 '13 at 16:17
  • @HowardPautz - I cannot get the torch to come on in my app for the Samsung Galaxy S5, even though it works on all other phones. I tested using OpenCamera and their code turns the torch on. I cannot find differences between the way I'm setting params and theirs. Only thing we can figure is a timing issue? Do you know anything about the camera event firing? I have a question here: http://stackoverflow.com/q/28160616/1735836 – Patricia Jan 29 '15 at 17:41