我写了这个测试程序来拍摄android中的照片
这是测试版,最终版本没有活动,因为程序将在后台运行
Camera camera;
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback(){
@Override
public void onShutter() {}
};
Camera.PictureCallback rawPictureCallback = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {}
};
Camera.PictureCallback jpegPictureCallback = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Log.v("takePhoto", "photo here");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.button);
camera=Camera.open();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
}
});
}
我收到了运行时错误
Process: com.myapplication, PID: 14355
java.lang.RuntimeException: takePicture failed
我也试过这个
camera=Camera.open();
camera.startPreview();
但仍然有同样的错误
如何使这个程序有效?
更新
这是完整的错误信息
Process: com.myapplication, PID: 15122
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1290)
at android.hardware.Camera.takePicture(Camera.java:1235)
at com.myapplication.MainActivity$4.onClick(MainActivity.java:58)
at android.view.View.performClick(View.java:4453)
at android.view.View$PerformClick.run(View.java:18464)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5064)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at dalvik.system.NativeStart.main(Native Method)