我在使用相机api在Android中拍照时遇到了麻烦。使用相机意图似乎工作正常,但不是直接调用api时。
错误:
java.lang.RuntimeException: takePicture failed at android.hardware.Camera.native_takePicture
代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraId = findFrontFacingCamera();
camera = Camera.open(cameraId);
Parameters params = camera.getParameters();
SurfaceView dummy=new SurfaceView(context);
try {
camera.setPreviewDisplay(dummy.getHolder());
camera.startPreview();
camera.takePicture(null, photoHandler, photoHandler);
} catch (IOException e) {
camera.stopPreview();
camera.release();
}
}
private PictureCallback photoHandler = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
}
}
清单
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
log cat output
07-17 10:16:02.523: D/MakePhotoActivity(14591): Camera found
07-17 10:16:02.773: D/dalvikvm(14591): threadid=1: still suspended after undo (sc=1 dc=1)
07-17 10:16:18.229: D/AndroidRuntime(14591): Shutting down VM
07-17 10:16:18.229: W/dalvikvm(14591): threadid=1: thread exiting with uncaught exception (group=0x40c4f930)
07-17 10:16:18.309: E/AndroidRuntime(14591): FATAL EXCEPTION: main
07-17 10:16:18.309: E/AndroidRuntime(14591): java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.fakelove.stationtostation/tv.fakelove.stationtostation.MainActivity}: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Looper.loop(Looper.java:137)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 10:16:18.309: E/AndroidRuntime(14591): at dalvik.system.NativeStart.main(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): Caused by: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.native_takePicture(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1095)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1040)
07-17 10:16:18.309: E/AndroidRuntime(14591): at tv.fakelove.stationtostation.MainActivity.onCreate(MainActivity.java:59)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Activity.performCreate(Activity.java:5104)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:1)
查看上一个问题中提供的答案:Android: "Camera.takePicture failed" Exception
显然你需要在拍照之前开始预览,其中包括设置一个有效的预览表面(我认为你没有这样做)。
另外,请查看http://developer.android.com/reference/android/hardware/Camera.html
中列出的步骤5-6
- 从open(int)获取Camera实例。
- 使用getParameters()获取现有(默认)设置。
- 如有必要,修改返回的Camera.Parameters对象并调用setParameters(Camera.Parameters)。
- 如果需要,请调用setDisplayOrientation(int)。
- 重要提示:将完全初始化的SurfaceHolder传递给setPreviewDisplay(SurfaceHolder)。没有表面,相机会 无法开始预览。
- 重要提示:调用startPreview()开始更新预览曲面。必须先开始预览才能拍照。
- 如果需要,请调用takePicture(Camera.ShutterCallback,Camera.PictureCallback,Camera.PictureCallback, Camera.PictureCallback)捕捉照片。等待回调 提供实际图像数据。
- 拍照后,预览显示将停止。要拍摄更多照片,请先再次调用startPreview()。
- 调用stopPreview()以停止更新预览表面。
- 重要事项:调用release()以释放相机以供其他应用程序使用。应用程序应立即释放相机 onPause()(并在onResume()中重新打开()。
醇>
有人想过 - 当你使用虚拟SurfaceView时,你需要设置SurfaceView的宽度/高度,以匹配相机预览尺寸的宽度/高度吗?
如果没有完整的堆栈跟踪,我可以看出你的代码错误了很多。