SecurityException: Permission Denial: reading (only on emulator)

核能气质少年 提交于 2019-11-29 01:19:12
riggaroo

What version of android is your emulator? And are you compiling with SDK 23? If your emulator is Marshmallow, you need to explicitly check for the permission at runtime. This is due to the new permissions model.

Check out the docs on permissions for more info: https://developer.android.com/training/permissions/index.html

Philip Herbert

Before calling method, check for permission.

private void checkPermission(){
    int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {     
         ActivityCompat.requestPermissions(         
    getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constants.WRITE_EXTERNAL_STORAGE); 
    } else {    
         callMethod(); 
    }
}

Check permission result.

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {

            case Constants.WRITE_EXTERNAL_STORAGE:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    callMethod(); 
                }
                break;

            default:
                break;
        }
    }
Swamy

If your app target version is 23 then it will give you this error, if your requirement includes API level 23 then you need to make target 23 and need to check permissions dynamically here is the example from developer site check https://developer.android.com/training/permissions/requesting.html

Note: Even if you give target 22 or lower also your app will work with 23 without this(check permission) error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!