Android API 23 Read_external_storage权限问题

时间:2016-05-10 11:04:47

标签: android android-camera android-permissions

我正在使用以下代码。但是当我点击添加照片按钮时没有任何事情发生。在println中我看到了4号和5号。我做错了什么。我在片段中使用代码。任何帮助都会很棒。

int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE;

// define in class
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    System.out.println("number 2");
    // We have access. Life is good.
} else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
    System.out.println("number 3");
    // We've been denied once before. Explain why we need the permission, then ask again.
} else {
    System.out.println("number 4");
    // We've never asked. Just do it.
    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    System.out.println("number 5");
}

回叫部分如下:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // We got the permission

        System.out.println("number 6");

    } else {

        System.out.println("number 7");

        // We were not granted permission this time, so don't try to show the contact picker
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

编辑:

我编写了如下代码,现在我在println日志中看到了数字4,5,7和8。没有错误,但没有任何反应。不应该打开一个对话框要求用户给予许可吗?我使用nexus 6和api 19作为模拟器。

private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 7;
// define in class

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    System.out.println("number 2");
    // We have access. Life is good.
} else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
    System.out.println("number 3");
    // We've been denied once before. Explain why we need the permission, then ask again.
} else {
    System.out.println("number 4");
    // We've never asked. Just do it.
    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    System.out.println("number 5");
}

回叫部分如下:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // We got the permission

        System.out.println("number 6");

    } else {

        System.out.println("number 7");

        // We were not granted permission this time, so don't try to show the contact picker
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        System.out.println("number 8");
    }
}

编辑2:

我没有添加对manifest的权限,我添加了<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />,现在看来还可以。

1 个答案:

答案 0 :(得分:0)

MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE必须是带有值的静态int,以便检查多个权限,并返回有效的比较结果。