Mahesh
Mahesh

Reputation: 1267

Camera not working when lock screen and return


i am working on a app which opens flash to preview image data and turns off the flash when done.

public void openFlash() {
    try {
        flash =false;
        previewing=false;
        finger_placed=false;

        Log.d("", "openFlash");
        if(camera==null){
            camera = application.getCamera();
            preview = Dashboard.preview;
            previewHolder = Dashboard.previewHolder;

        }
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

        camera.setParameters(parameters);
        camera.setPreviewDisplay(previewHolder);
        camera.setPreviewCallback(previewCallback);
        camera.startPreview();
    } catch (Throwable t) {
        Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t);
    }
}

public void stopPreview() {

    try{
        Log.d("", "stopPreview");
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
        camera.stopPreview();
        previewing=false;
    }catch (Exception e) {
        e.printStackTrace();
    }
}

every thing works fine untill i lock the screen.WHen i unlock the screen and use app. openFlash doesnot works. and gives error as below

06-20 12:13:25.725: E/AndroidRuntime(29033): FATAL EXCEPTION: main

06-20 12:13:25.725: E/AndroidRuntime(29033): java.lang.RuntimeException: Method called after release()
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.hardware.Camera.setHasPreviewCallback(Native Method)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.hardware.Camera.access$600(Camera.java:133)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:805)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.os.Looper.loop(Looper.java:137)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
06-20 12:13:25.725: E/AndroidRuntime(29033):     at dalvik.system.NativeStart.main(Native Method)

Does anybody came across this kind of situation. any help will be appreciated Thanks.

Upvotes: 2

Views: 3410

Answers (5)

Mahesh
Mahesh

Reputation: 1267

I feagured it out. Need to handle lifecycle events to open and release the camera and setRespective parameters

Upvotes: 1

npace
npace

Reputation: 4258

In your activity's onPause(), you need to call release() on your Camera object and in your onResume, you need to call open() on it respectively.

From the API reference:

Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

Upvotes: 1

David C. Sainte-Claire
David C. Sainte-Claire

Reputation: 2481

You should not be setting a wake lock because you'll burn through the device battery, unless you're trying to keep the device from suspending. Which it doesn't seem like you are. I can't see your lifecycle events (onCreate,onPause,etc), but I would bet that you are opening the camera in onCreate and releasing it in onPause, which would get called after the screen gets locked. But when you unlock the screen, your app may not have been destroyed yet,so onResume gets called immediately; meaning that you never reopen the camera. Without seeing any of your other code, I'd start looking at where you open and release the camera

Upvotes: 0

Yogendra
Yogendra

Reputation: 5268

Did you define permission regarding camera and flash in manifest file. example: android:name="android.permission.FLASHLIGHT" android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" if you are sure about this then check your camera object is release after stopPreview(). Example: _camera.stopPreview(); _camera.release();

Upvotes: 0

hitman_93
hitman_93

Reputation: 116

One of the ways is you could acquire the wake lock while your app is working so that you ensure that the android device never gets locked.To do this just add permission :-

<uses-permission android:name="android.permission.WAKE_LOCK" />

in the manifest and as soon as your app starts add this in onCreate() :-

 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");

while in onResume():-

 wl.acquire();

and in onPause() add this :-

 wl.release();

Or rather another method to do this would be simply do :-

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

Upvotes: -1

Related Questions