我只需要一種方法來獲得使用智能手機相機的權限。 我寫了這段代碼,但沒有出現權限請求對話框。如何授予相機權限?
在OnCreate:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// my code after granted permission
} else {
}
return;
}
}
}
當您在調試器中逐步瀏覽代碼時,您學到了什麼?你真的調用'requestPermissions()'嗎? – CommonsWare
@CommonsWare不,requestPermissions()不會被調用,因爲「ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)」爲true。 – xRobot