android Camera里面的问题:fail to connect to Camera service - 开源中国社区
当前访客身份:游客 [
当前位置:
public class CameraActivity extends Activity implements Callback { &private Button btnClose, btnPhoto, btnP &private SurfaceV &private SurfaceH &private C &private boolean ispreview = &private String picturePath = &/sdcard/zkPic/carema1.jpg&; &private ShutterCallback shuttercallback = new ShutterCallback() {
&&@Override &&public void onShutter() { &&&// TODO Auto-generated method stub
&&} &}; &private PictureCallback rawcallback = new PictureCallback() {
&&@Override &&public void onPictureTaken(byte[] data, Camera camera) { &&&// TODO Auto-generated method stub
&&} &}; &private PictureCallback picturecallback = new PictureCallback() {
&&@Override &&public void onPictureTaken(byte[] data, Camera camera) { &&&// TODO Auto-generated method stub &&&Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); &&&File picFile = new File(picturePath); &&&try { &&&&BufferedOutputStream bos = new BufferedOutputStream( &&&&&&new FileOutputStream(picFile)); &&&&pressFormat.JPEG, 80, bos); &&&&resetCamera(); &&&&initCamera(); &&&} catch (Exception e) { &&&&System.out.println(e.toString()); &&&}
&public void setupView() { &&this.btnClose = (Button) findViewById(R.id.btnClose); &&this.btnPhoto = (Button) findViewById(R.id.btnPhoto); &&this.btnPreview = (Button) findViewById(R.id.btnPreview); &&this.surfaceview = (SurfaceView) findViewById(R.id.surfaceView); &&this.surfaceholder = surfaceview.getHolder(); &&surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); &}
&@Override &protected void onCreate(Bundle savedInstanceState) { &&// TODO Auto-generated method stub &&super.onCreate(savedInstanceState); &&setContentView(R.layout.camera); &&setupView(); &&DisplayMetrics dm = new DisplayMetrics(); &&getWindowManager().getDefaultDisplay().getMetrics(dm); &&btnPreview.setOnClickListener(new OnClickListener() {
&&&@Override &&&public void onClick(View v) { &&&&// TODO Auto-generated method stub &&&&initCamera(); &&&} &&}); &&btnClose.setOnClickListener(new OnClickListener() {
&&&@Override &&&public void onClick(View v) { &&&&// TODO Auto-generated method stub &&&&resetCamera(); &&&} &&}); &&btnPhoto.setOnClickListener(new OnClickListener() {
&&&@Override &&&public void onClick(View v) { &&&&// TODO Auto-generated method stub &&&&takePhoto(); &&&} &&}); &}
&private void initCamera() { &&if (ispreview==false&&camera==null) { &&&camera = Camera.open(); &&} &&if (camera != null && !ispreview) { &&&try { &&&&/* &&&& * Camera.Parameters parameters = camera.getParameters(); &&&& * parameters.setPreviewSize(320, 240); &&&& * parameters.setPictureSize(320, 240); &&&& * camera.setParameters(parameters); &&&& */ &&&&camera.setPreviewDisplay(surfaceholder); &&&&camera.startPreview();
&&&} catch (Exception e) { &&&&// TODO: handle exception &&&} &&&ispreview = &&} &}
&private void takePhoto() { &&if (camera != null && ispreview) { &&&camera.takePicture(shuttercallback, rawcallback, picturecallback); &&} &}
&private void resetCamera() { &&if (camera != null && ispreview) { &&&camera.stopPreview(); &&&camera.release(); &&&camera = &&&ispreview = &&} &&finish(); &}
&@Override &public void surfaceChanged(SurfaceHolder holder, int format, int width, &&&int height) { &&// TODO Auto-generated method stub
&@Override &public void surfaceCreated(SurfaceHolder holder) { &&// TODO Auto-generated method stub
&@Override &public void surfaceDestroyed(SurfaceHolder holder) { &&// TODO Auto-generated method stub
05-08 15:43:56.493: E/AndroidRuntime(2998): FATAL EXCEPTION: main 05-08 15:43:56.493: E/AndroidRuntime(2998): java.lang.RuntimeException: Fail to connect to camera service 05-08 15:43:56.493: E/AndroidRuntime(2998): &at android.hardware.Camera.native_setup(Native Method) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at android.hardware.Camera.&init&(Camera.java:258) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at android.hardware.Camera.open(Camera.java:220) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at com.zk.pic.CameraActivity.initCamera(CameraActivity.java:111) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at com.zk.pic.CameraActivity.access$2(CameraActivity.java:109) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at com.zk.pic.CameraActivity$4.onClick(CameraActivity.java:88) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at android.view.View.performClick(View.java:2486) 05-08 15:43:56.493: E/AndroidRuntime(2998): &at android.view.View$PerformClick.run(View.java:9130)
补充:权限加过了
&user-permission android:name=&android.permission.CAMERA&/& &&& &user-permission android:name=&android.permission.INTERNET&/&
共有6个***