在清单和运行时中允许读写权限后拒绝权限

时间:2019-05-31 12:48:27

标签: java android

我收到此错误

   W/System.err: java.io.FileNotFoundException: /root/storage/emulated/0/DCIM/Camera/IMG_20181116_172404.jpg (Permission denied) 

我在清单中添加了以下内容:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在MainActivity.java中添加了以下代码

    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
    private void checkIfPermissionGranted() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if(hasWritePermission != PackageManager.PERMISSION_GRANTED){
                requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        REQUEST_CODE_ASK_PERMISSIONS);
                return;
            }
            if(hasReadPermission != PackageManager.PERMISSION_GRANTED){
                requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
                        REQUEST_CODE_ASK_PERMISSIONS);
                return;
            }
        }

    }

该如何解决?这是设置菜单中应用权限的屏幕截图

1 个答案:

答案 0 :(得分:0)

  // Implement this overide method..

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

        if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {


    //  implement Your File related Code here,

        } else {
            Toast.makeText(this, "Permission is Require for Access External Storage", Toast.LENGTH_SHORT).show();
        }

    }