问题描述
我正在修改现有的Face Tracker应用程序Android的面部识别示例项目.我在请求多个永久权限时遇到问题.下面的方法是现有方法的修改版本,该方法成功创建了一个弹出窗口以请求相机许可.我正在尝试使用存储权限复制此文件,但到目前为止,我一直没有成功,而且我不确定在这里需要更改什么.
I'm modifying an existing Face Tracker app Android's Facial Recognition sample projects. I'm having an issue with requesting multiple permanent permissions. The method below is a modified version of the existing method that successfully creates a pop up window to ask for camera permissions. I'm trying to replicate this with the storage permissions but so far I've been unsuccessful and I'm not sure what needs to be changed here.
private void requestAllPermissions() {
Log.w(TAG, "Camera + Storage permissions are not granted. Requesting permissions");
final String[] permissions = new String[]{Manifest.permission.CAMERA};
final String[] permissions2 = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
return;
}
//new
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this, permissions2, RC_HANDLE_STORAGE_PERM);
return;
}
final Activity thisActivity = this;
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(thisActivity, permissions,
RC_HANDLE_CAMERA_PERM);
}
};
View.OnClickListener listener2 = new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(thisActivity, permissions2,
RC_HANDLE_STORAGE_PERM);
}
};
Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, listener)
.show();
Snackbar.make(mGraphicOverlay, R.string.permission_storage_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, listener2)
.show();
}
推荐答案
如果要在一个对话框中询问所有权限,则应该只有一个String数组,如下所示:
you should have only one String array if you want to ask all permissions in one dialog box, like this:
int ALL_PERMISSIONS = 101;
final String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, permissions, ALL_PERMISSIONS);
这篇关于询问多个权限Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!