我将相机设置为this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
并且工作正常但是如果我将其更改为PORTRAIT
而不是LANDSCAPE
,那么它会因以下错误而崩溃...
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): FATAL EXCEPTION: main
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): java.lang.RuntimeException: Fail to connect to camera service
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.native_setup(Native Method)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.<init>(Camera.java:110)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.open(Camera.java:90)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at org.digital.com.CamLayer.surfaceCreated(CamLayer.java:3
它崩溃的方法是..
public void surfaceCreated(SurfaceHolder holder) {
synchronized(this) {
mCamera = Camera.open();
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(800, 480);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e("Camera", "mCamera.setPreviewDisplay(holder);");
}
mCamera.startPreview();
mCamera.setPreviewCallback(this);
}
}
它在mCamera = Camera.open();
我的清单文件有<uses-permission android:name="android.permission.CAMERA"></uses-permission>
如何解决此问题,以便我可以在肖像中查看我的应用程序?
答案 0 :(得分:24)
平台中存在一些并发问题:http://code.google.com/p/android/issues/detail?id=6201
解决方法是在释放cam之前清除回调,我建议遵循清理代码:
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
答案 1 :(得分:16)
供您参考,这是我在我的应用程序中使用的SurfaceHolderCallBack内部类,它在纵向模式下可以在Nexus One 2.2上正常工作 - 希望它有所帮助。
编辑:“哪个有效”=“哪个没有崩溃”。虽然我还没弄清楚如何正确旋转预览图像(请参阅上面的第一条评论)。这就是为什么我实际上不得不使用横向和'转换'围绕预览表面的UI内容到横向模式。 Afaik预览(正确旋转预览图像)仅适用于横向模式。旋转&amp;我试过的方向参数根本没用。
class SurfaceHolderCallback implements SurfaceHolder.Callback {
private static final int IMAGE_WIDTH = 512;
private static final int IMAGE_HEIGHT = 384;
private static final String ORIENTATION = "orientation";
private static final String ROTATION = "rotation";
private static final String PORTRAIT = "portrait";
private static final String LANDSCAPE = "landscape";
public void surfaceCreated(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);
Parameters p = camera.getParameters();
p.setPictureSize(IMAGE_WIDTH, IMAGE_HEIGHT);
camera.getParameters().setRotation(90);
Camera.Size s = 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);
}
}
public void surfaceChanged(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;
}
public void surfaceDestroyed(SurfaceHolder arg0) {
if(isPreviewRunning && camera != null) {
if(camera!=null) {
camera.stopPreview();
camera.release();
camera = null;
}
isPreviewRunning = false;
}
}
}
答案 2 :(得分:13)
你在AndroidManifest.xml中有这个设置吗?
uses-permission android:name="android.permission.CAMERA"
答案 3 :(得分:2)
而不是使用:
<uses-permission
android:name="android.permission.FLASHLIGHT"/>
尝试使用:
<uses-permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
并且不要忘记添加相机权限:
<uses-permission android:name="android.permission.CAMERA" />
答案 4 :(得分:0)
我发现您已将预览区域设置为800 x 480.在纵向模式下,此大小无效,并且API中可能存在导致其崩溃的错误。
尝试设置200x200的预览区域
答案 5 :(得分:0)
我认为您的问题是,当您旋转相机时,它无法正确释放或根本无法释放。如果您从landscape
切换到portrait
,则会再次启动Intent。如果此时相机未释放并重新打开,则尝试打开已打开的相机,这会引发错误。
答案 6 :(得分:0)
如果您使用的是Android模拟器,另一种可能是Android虚拟设备设置中前后摄像头被定义为“无”。
答案 7 :(得分:0)
这可能有用......
@Override public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surfaceDestroyed", "");
cameraSource.stop(); //check if you have this line
}