您好我在我的onMenuItemClick中调用ActivityCompat.requestPermission来授予WRITE_EXTERNAL_STORAGE权限。但是,不执行ActivityCompat.requestPermissions。
我的目标是API 23
我在清单中定义了
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
这是我在onMenuItemClick
中调用的代码 case R.id.id_captura:
if (ContextCompat.checkSelfPermission(ChatActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(ChatActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(ChatActivity.this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_CTE
);
}
}
break;
这是我的onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case WRITE_EXTERNAL_STORAGE_CTE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this).setTitle("Permissions").setMessage("Permissions not granted ").setPositiveButton("Accept", null);
alertDialog.show();
}
return ;
}
}
}
我定义了
private final int WRITE_EXTERNAL_STORAGE_CTE = 1;
答案 0 :(得分:0)
问题是android:maxSdkVersion="18"
,因为我的目标是api 23,权限是在运行时请求的,但也必须在清单文件中声明。
删除该行就完成了这项工作。