黑屏调用摄像头功能

时间:2016-01-14 16:03:59

标签: java android camera

我在Android Studio上的Fragment Class上实现了Mediarecorder功能,并在我的Activity类上调用了Camera Fragment类,在我的移动设备上编译后,显示为黑屏相机,如下所示:

CameraFragment.java

private void prepareRecorder() {

    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        //finish();
    } catch (IOException e) {
        e.printStackTrace();
        //finish();
    }
}

我无法清楚地看到问题出在哪里,也许就在这里:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view001 = inflater.inflate(R.layout.fragment_camera,container,false);
    activity = getActivity();
    cameraManager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    recorder = new MediaRecorder();
    initRecorder();
    btnStartRec = (Button) view001.findViewById(R.id.btnCaptureVideo);
    btnStartRec.setOnClickListener(this);
    SurfaceView cameraView = (SurfaceView)view001.findViewById(R.id.surfaceCamera);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    cameraView.setClickable(true);
    cameraView.setOnClickListener(this);

    return view001;
}

MainActivity类,我称之为片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensors);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    camera = new CameraFragment();
    getFragmentManager().beginTransaction().replace(R.id.camLayout, camera).commit();
    RelativeLayout lay = (RelativeLayout) findViewById(R.id.camLayout);
    lay.setVisibility(View.VISIBLE);

    arquivo = inicializaArquivo(arquivo);

    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    final Button stopButton = (Button) findViewById(R.id.stopButton);
    stopButton.setVisibility(View.GONE);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            iniciarSensores();
            fab.setVisibility(View.GONE);
            stopButton.setVisibility(View.VISIBLE);
        }
    });

你们可以帮我解决这个问题吗,我无法在SurfaceView上显示相机视图,谢谢:

Obs:我正在使用Android Studio版本1.51

修改:我已在AndroidManifest.xml

上宣布了相机权限

0 个答案:

没有答案