if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
// Do code here
} else {
// Request permission here
}
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) {
Snackbar.make(mAnchorLayout, "Camera permission is needed to attach pictures",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}
})
.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Snackbar.make(mAnchorLayout, "You can now take photos",
Snackbar.LENGTH_SHORT).show();
} else { Snackbar.make(mAnchorLayout, "Permission not granted",
Snackbar.LENGTH_SHORT).show();
}
}
}