当我在sdcard中尝试createFile时:
newFile.createNewFile();
我得到的日志如下:
W / System.err:java.io.IOException:Permission denied
W / System.err:at java.io.UnixFileSystem.createFileExclusively0(本机方法) W / System.err:at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280) W / System.err:在java.io.File.createNewFile(File.java:948)
我知道原因是没有授予危险的运行时权限(WRITE_EXTERNAL_STORAGE),但是当我尝试在PackageManagerService.java checkUidPermission()
中打印日志时,发现它没有被调用。
我想知道本机方法(如java.io.UnixFileSystem.createFileExclusively0
)如何判断未授予运行时权限?
答案 0 :(得分:3)
从Android M,用户可以授予/撤销权限,而不是安装时间。
因此,当定位具有M及更高版本的设备时,您需要检查是否已授予权限我使用此类检查权限,我希望它有所帮助:
/**
* Created by Mani on 17/09/16.
* Making the runtime app permissions for android marshmallow+ devices cleaner
* Specify all the requests here
*/
public class MarshMallowPermission {
private static final int CALL_PERMISSION_REQUEST_CODE = 1;
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
public static final int SHORT_CUT_PERMISSION_REQUEST_CODE = 4;
private Activity activity;
public MarshMallowPermission(Activity activity) {
this.activity = activity;
}
public boolean checkPermissionForCall(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE);
return result == PackageManager.PERMISSION_GRANTED;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public boolean checkPermissionForShortCut(){
int result = PermissionChecker.checkSelfPermission(activity,Manifest.permission.INSTALL_SHORTCUT);
return result == PermissionChecker.PERMISSION_GRANTED;
}
public boolean checkPermissionForExternalStorage(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readStorage = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
readStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
}
return result == PackageManager.PERMISSION_GRANTED && readStorage == PackageManager.PERMISSION_GRANTED;
}
public boolean checkPermissionForCamera(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
return result == PackageManager.PERMISSION_GRANTED;
}
public void requestPermissionForCall(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CALL_PHONE)){
Toast.makeText(activity, "Call Phone Permission required to launch Dialer. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CALL_PHONE},CALL_PERMISSION_REQUEST_CODE);
}
}
public void requestPermissionForExternalStorage(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(activity, "External Storage permission required. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
}else{
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
}
}
}
public void requestPermissionForCamera(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
Toast.makeText(activity, "Camera permission required. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
}
}
public void requestPermissionForShortCut(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.INSTALL_SHORTCUT)){
Toast.makeText(activity, "Create ShortCut permission required. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.INSTALL_SHORTCUT},SHORT_CUT_PERMISSION_REQUEST_CODE);
}
}
}
活动内部使用:
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(MyActivity.this);
marshMallowPermission.checkPermissionForCall();
marshMallowPermission.requestPermissionForCall();
如果您有任何疑问,请在下方评论。