Android onPictureTaken to ImageView

时间:2015-09-28 12:20:10

标签: java android camera

我的目标是拥有一个带有快照按钮(imgSnap)的自定义相机,当按下此按钮时,我希望应用程序转到另一个意图(CameraReview),其中图像显示在imageview中,此处用户可以选择保持或删除。代码波纹管工作,直到我开始审查意图,它只是暂停相机预览和休息。我无法找到原因。

Button imgSnap = (Button)findViewById(R.id.imgSnap);
            imgSnap.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCamera.takePicture(null, null, null, new Camera.PictureCallback() {

                        @Override
                        public void onPictureTaken(byte[] data, Camera camera) {
                            Toast.makeText(getApplicationContext(), "Picture Taken",
                                    Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(x, CameraReview.class);
                            intent.putExtra("image_arr", data);
                            setResult(RESULT_OK, intent);
                            webView.startActivity(intent);
                            camera.stopPreview();
                            if (camera != null) {
                                camera.release();
                                mCamera = null;
                            }
                            finish();
                        }
                    });
                }
            });

编辑: 抛出以下异常:

Camera is being used after Camera.release() was called

1 个答案:

答案 0 :(得分:1)

看来你有两个不同的Camera实例。其中一个看起来与您的代码无关。

尝试更改

     camera.stopPreview(); 
     if (camera != null) { 
     camera.release(); 
     mCamera = null; 
     } 

 mCamera.stopPreview(); 
 if (mCamera != null) { 
 mCamera.release(); 
 mCamera = null; 
 }