检查权限有没有
private void checkPermission(){
if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else {
// 有权限
showPdf();
}
}
权限申请结果回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1){
if (grantResults[0]==0){
showPdf();
}else if (grantResults[0]==-1){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
// 向用户展示申请权限的理由 再次申请
Toast.makeText(this,"文件读取权限",Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
}
}