Почему textureView показывает черный экран на моем приложении камеры в Android API уровня 23?

Я разрабатываю приложение камеры, используя TextureView учебный класс.

Но черный экран отображается, когда MainActivity создано.

Я уже проверил некоторые исходники на Github, но у них та же проблема.

//MainActivity
private CaptureFragment     mCaptureFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    mCaptureFragment = CaptureFragment.newInstance();
        getFragmentManager().beginTransaction()
                .replace(R.id.recordVideoContent, mCaptureFragment)
                .commit();
    //...
}

//CaptureFragment class
public class CaptureFragment Fragment {
    private AutoFitTextureView mCameraLayout;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mCameraLayout = (AutoFitTextureView)               
                                 view.findViewById(getTextureResource());
}
@Override
public void onResume() {
    super.onResume();
    if (mCameraLayout.isAvailable()) {
        openCamera(mCameraLayout.getWidth(), mCameraLayout.getHeight());
    } else {
        mCameraLayout.setSurfaceTextureListener(mSurfaceTextureListener);
    }
}

private TextureView.SurfaceTextureListener mSurfaceTextureListener = new 
                                  TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, 
                        int height) {
        openCamera(width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int 
                                                      width, int height) {
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        Log.d("SurfaceTextureUpdated", "");
    }
};

private void openCamera(int width, int height, int mode) {
    if (!CameraUtil.hasPermissionsGranted(getActivity(), 
                        Camera2PermissionDialog.VIDEO_PERMISSIONS)) {
        requestVideoPermissions();
        return;
    }
    CameraManager cameraManager = (CameraManager) 
              activity.getSystemService(Context.CAMERA_SERVICE);
    try {

    } catch() {
        //...
        cameraManager.openCamera(cameraId, mStateCallback, null);
        //...
    }
}

private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {
        mCameraDevice = camera;

        startPreview();
        mCameraOpenCloseLock.release();
        if (null != mCameraLayout) {
            configureTransform(mCameraLayout.getWidth(), mCameraLayout.getHeight());
        }
    }

    @Override
    public void onDisconnected(@NonNull CameraDevice camera) {
        mCameraOpenCloseLock.release();
        camera.close();
        mCameraDevice = null;
    }

    @Override
    public void onError(@NonNull CameraDevice camera, int error) {
        mCameraOpenCloseLock.release();
        camera.close();
        mCameraDevice = null;
        Activity activity = getActivity();
        if (null != activity) {
            activity.finish();
        }
    }
};

0 ответов

Другие вопросы по тегам