相机不保存图像并丢弃异常

时间:2018-03-06 08:49:10

标签: java android android-fragments camera fileoutputstream

我正在创建相机应用。我是以片段形式做到的。我有FrameLayout我添加了相机,还有一个按钮用于捕捉。有两个问题。

1)相机不保存图像。

2)当我第二次点击捕获图像时,删除异常java.lang.RuntimeException: takePicture failed

我不知道问题出在哪里,我第一次使用相机API。你能帮我找到解决方案吗?好比谢谢。

相机i仅在纵向模式下使用。在清单中,我添加了摄像头和写入外部存储的权限。

我有一节课我在SurfaceView延伸。让我告诉你代码。

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback{

Camera camera;
SurfaceHolder holder;

public ShowCamera(Context context, Camera camera) {
    super(context);
    this.camera = camera;
    holder = getHolder();
    holder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    Camera.Parameters params = camera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;

    for (Camera.Size size : sizes){
        mSize = size;
    }

    camera.setDisplayOrientation(90);
    params.setRotation(90);

    params.setPictureSize(mSize.width, mSize.height);
    camera.setParameters(params);
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    camera.stopPreview();
    camera.release();
}
}

好的,这是相机片段。

public class CameraActivity extends BaseFragment{
View mainView;
FrameLayout cameraLayout;
Camera camera;
ShowCamera showCamera;
ImageButton captureImage;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.camera_fragment, container, false);

    cameraLayout = (FrameLayout) mainView.findViewById(R.id.cameraContainer);
    captureImage = (ImageButton) mainView.findViewById(R.id.imageButton);

    camera = Camera.open();
    showCamera = new ShowCamera(getActivity(), camera);

    cameraLayout.addView(showCamera);

    capture();

    return mainView;
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] bytes, Camera camera) {
        if(bytes != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            if(bitmap != null){
                File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
                if(!file.isDirectory()){
                    file.mkdir();
                }

                file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
                        System.currentTimeMillis() + ".jpg");

                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
};

public void capture(){
    captureImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(camera != null){
                camera.takePicture(null, null, mPictureCallback);
            }
        }
    });

}
}

帮我找到解决方案。谢谢阅读。

0 个答案:

没有答案