Also please tell me if I can display a dialog asking some permission
for using the camera like in marshmallow 6.0. I need such thing to do
in device below marshmallow 23 api. Like launching the camera app
should ask the permission in a dialog and if I click on no it should
close the camera app
For Android Marshmallow , whenever you want to launch camera you should check whether you have permissions for accessing camera and if not you can request for permissions by showing the system dialog -
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 1001); //May be any number
} else {
//Launch camera since you have the permission
}
Also, you have to implement onRequestPermissionsResult()
callback so that you can check the user action on the permission dialog and react accordingly -
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1001 : //Same request code
if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,"Camera permission requied", Toast.LENGTH_LONG).show();
} else if (grantResults.length > 0 && (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
//Launch camera
}
break;
}
}