首先在manifests中添加所需权限,比如拍照权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然后再Activity中添加代码
String[] PERMISSIONS = {"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE"};
//检测是否有拍照和写存储的权限
int permission = ContextCompat.checkSelfPermission(MainActivity.this, "android.permission.CAMERA"+"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有拍照和写存储的权限,去申请权限,会弹出对话框
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS,1);
}