Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W/CameraBase﹕ An error occurred while connecting to camera: 0

There was a similar question but it went unresolved:

W/CameraBase﹕ An error occurred while connecting to camera: 0 on camera.open() call

I have an app which (naturally) is using the camera. Its been working fine. However with no code changes, between builds, the app started crashing (verified no code changes via Git commit history).

To further verify the issue is somewhere in the hardware, I ran the same code on another phone and it works just fine. Somehow the camera is locked and won't open.

Here is the relevant code:

Create a previe object

_cameraPreview = new CameraPreview(getActivity(),_camera);

This is the creation code for it

 public CameraPreview(Context context, Camera camera) 
    {
        super(context);
        _camera = camera;
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);
    }

Opening the camera:

//Opens back facing camera by default

public static Camera getCameraInstance()
{
    Camera c = null;
    try
    {
        c = Camera.open();
    }catch (Exception e)
    {
       e.printStackTrace();
    }

    return c;
}

This is where the error occurs, camera.Open throws the error with the following stack trace. My camera object is null. This code is from the official Android docs but they don't talk about how to 'release the camera' if its locked from outside the session.

The full log out is

W/CameraBase﹕ An error occurred while connecting to camera: 0
W/System.err﹕ java.lang.RuntimeException: Fail to connect to camera service
W/System.err﹕ at android.hardware.Camera.<init>(Camera.java:497)
W/System.err﹕ at android.hardware.Camera.open(Camera.java:357)
W/System.err﹕ at co.pumpup.app.EditPhotoFragment.getCameraInstance(EditPhotoFragment.java:241)
W/System.err﹕ at co.pumpup.app.EditPhotoFragment.onCreateView(EditPhotoFragment.java:76)
W/System.err﹕ at android.app.Fragment.performCreateView(Fragment.java:2053)
W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
W/System.err﹕ at android.app.BackStackRecord.run(BackStackRecord.java:834)
W/System.err﹕ at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
W/System.err﹕ at android.app.Activity.performStart(Activity.java:6005)
W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Can you advise how I can 'unlock' a locked up camera like this?

like image 930
Aggressor Avatar asked May 02 '15 00:05

Aggressor


2 Answers

The "fail to connect to camera service" means that your (or some other) camera app has failed to release camera properly. This was happening to me occasionally as I was developing my app, too. The first thing I'd try is to open the camera from the default camera app that comes with the phone (just tapping the "camera" button). If that would fail to open, then the only option was to restart the phone.

This is speculation, but your code might have a bug that surfaces only occasionally and it just didn't appear until now through pure luck. If it persist, check out the questions related to "how to release camera properly in android". They had helped me to deal with this.

like image 79
panonski Avatar answered Oct 15 '22 16:10

panonski


Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen. http://developer.android.com/training/permissions/requesting.html

like image 44
Andrea Motto Avatar answered Oct 15 '22 17:10

Andrea Motto