0

I am developing a camera app in android (without using intent) where , i am implementing both auto focus and Flash light features . It works fine if i implement any one feature. But on adding both Auto focus and Flash Light feature, it gives force close in LG nexus and other phones which has Flash Light. i am using the below code for auto focus and Flash Light.

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        event.startTracking();
        camera.autoFocus(autoFocusCallback);
        Parameters p = camera.getParameters();
        p.setFocusMode(Parameters.FOCUS_MODE_AUTO);

        if(this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
        {

            p.setFlashMode(Parameters.FLASH_MODE_ON);
            camera.setParameters(p);
            camera.startPreview();
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
        }

        else
        {
            camera.startPreview();
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
        }
        return true;

    }
    return super.onKeyDown(keyCode, event);
}

     private AutoFocusCallback autoFocusCallback = new AutoFocusCallback() 
    {

     @Override
     public void onAutoFocus(boolean autoFocusSuccess, Camera camera)
      {
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
              if (autoFocusHandler != null)
              {
                 Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
                 autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS);
                  autoFocusHandler = null;
               }
               else
               {

               }
    }
};

This is the Error Log i am getting.

http://txtup.co/WCYjl

What may be the problem. Please Help! Thanks!

sanjana
  • 641
  • 2
  • 15
  • 36

1 Answers1

0

Just had a quick read through the Camera docs, and it looks like you've missed the Surface Holder.

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

Have a read through the docs: http://developer.android.com/reference/android/hardware/Camera.html

Here is an answer which shows how to use this: https://stackoverflow.com/a/3964460/2045570

Community
  • 1
  • 1
nedaRM
  • 1,837
  • 1
  • 14
  • 28
  • can you please post the rest of your code? Also where is this happening? What is on line 914? – nedaRM Aug 27 '13 at 04:55
  • Its giving force close on this line. camera.setParameters(p); – sanjana Aug 27 '13 at 05:40
  • Whatever you're doing, you are choosing the wrong parameters for the device. I can help you if you post the rest of your code where you set values for p (eg: anything similar to p.setFocusMode(Parameters.FLASH_MODE_AUTO);) – nedaRM Aug 27 '13 at 05:49
  • Or before you set caemra Parameters , you can use camera.getParameters().flatten() to log all the parameters that the camera supports. So you can use the right parameter. – nedaRM Aug 27 '13 at 05:50
  • @sanjana check out my revised answer. – nedaRM Aug 27 '13 at 06:53