Fail To Connect To Camera Service
Solution 1:
There is some concurrency problem in platform: http://code.google.com/p/android/issues/detail?id=6201
The workaround is to clear callback before releasing cam, that is I would recommend following clean-up code:
@OverridepublicvoidsurfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
Solution 2:
For your reference, this is the SurfaceHolderCallBack inner class that I'm using in my app and which works fine on Nexus One 2.2 in portrait mode - hope it helps.
Edit: "which works" = "which doesn't crash". Although I haven't figured out how to rotate the preview image correctly (see my first comment above). That's why I actually had to use landscape and 'convert' UI stuff that's surrounding the preview surface into landscape mode. Afaik preview (with correct rotation of the preview image) only works in landscape mode. Rotation & orientation params that I tried didn't help at all.
classSurfaceHolderCallbackimplementsSurfaceHolder.Callback {
privatestaticfinalintIMAGE_WIDTH=512;
privatestaticfinalintIMAGE_HEIGHT=384;
privatestaticfinalStringORIENTATION="orientation";
privatestaticfinalStringROTATION="rotation";
privatestaticfinalStringPORTRAIT="portrait";
privatestaticfinalStringLANDSCAPE="landscape";
publicvoidsurfaceCreated(SurfaceHolder holder) {
try {
// This case can actually happen if the user opens and closes the camera too frequently.// The problem is that we cannot really prevent this from happening as the user can easily// get into a chain of activites and tries to escape using the back button.// The most sensible solution would be to quit the entire EPostcard flow once the picture is sent.
camera = Camera.open();
} catch(Exception e) {
finish();
return;
}
//Surface.setOrientation(Display.DEFAULT_DISPLAY,Surface.ROTATION_90);Parametersp= camera.getParameters();
p.setPictureSize(IMAGE_WIDTH, IMAGE_HEIGHT);
camera.getParameters().setRotation(90);
Camera.Sizes= p.getSupportedPreviewSizes().get(0);
p.setPreviewSize( s.width, s.height );
p.setPictureFormat(PixelFormat.JPEG);
p.set("flash-mode", "auto");
camera.setParameters(p);
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (Throwable ignored) {
Log.e(APP, "set preview error.", ignored);
}
}
publicvoidsurfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewRunning) {
camera.stopPreview();
}
try {
camera.startPreview();
} catch(Exception e) {
Log.d(APP, "Cannot start preview", e);
}
isPreviewRunning = true;
}
publicvoidsurfaceDestroyed(SurfaceHolder arg0) {
if(isPreviewRunning && camera != null) {
if(camera!=null) {
camera.stopPreview();
camera.release();
camera = null;
}
isPreviewRunning = false;
}
}
}
Solution 3:
Do you have this set in AndroidManifest.xml ?
uses-permission android:name="android.permission.CAMERA"
Solution 4:
Instead of using :
<uses-permissionandroid:name="android.permission.FLASHLIGHT"/>
Try to use:
<uses-permissionandroid:name="android.permission.FLASHLIGHT"android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"android:protectionLevel="normal" />
and don't forget to add the permission for camera:
<uses-permissionandroid:name="android.permission.CAMERA" />
Solution 5:
Another possibility if you're using an Android Emulator may be that the front and back cameras are defined as "none" in the Android Virtual Device settings.
Post a Comment for "Fail To Connect To Camera Service"