@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRxPermissions = RxPermissions.getInstance(this); setContentView(R.layout.act_main); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView); }
public void enableCamera(View v) { mRxPermissions .shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) .flatMap( should -> { if (should) { // User already denied the permission, but didn't // checked "never ask again". Toast.makeText( MainActivity.this, "Please please grant this permission !", Toast.LENGTH_SHORT) .show(); } return mRxPermissions.request(Manifest.permission.CAMERA); }) .subscribe( granted -> { if (granted) { releaseCamera(); mCamera = Camera.open(0); try { mCamera.setPreviewDisplay(mSurfaceView.getHolder()); mCamera.startPreview(); } catch (IOException e) { Log.e(TAG, "Error while trying to display the camera preview", e); } } else { Toast.makeText( MainActivity.this, "Permission denied, can't enable the camera", Toast.LENGTH_SHORT) .show(); } }); }