Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...