权限拒绝:需要android.permission.READ_EXTERNAL_STORAGE或grantUriPermission()(API 23)

时间:2016-10-10 11:43:09

标签: android android-permissions android-external-storage

我正在尝试在运行时请求用户权限。 API是23,我想从手机的图库中提取图像。在一些片段之后,这是我到目前为止的代码:

在活动的onCreate()我检查:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED) {
        imageUploader5.setEnabled(true);
        ActivityCompat.requestPermissions(this, new String[]
                { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }

然后我覆盖:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            imageUploader5.setEnabled(true);
        }
    }
}

但我还是无法让应用程序在AVD上运行。

编辑:清单中的权限:

<uses-permission-sdk-23 android:name="android.permission.CAMERA" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.READ_EXTERNAL_STORAGE" />

4 个答案:

答案 0 :(得分:4)

根据您构建if状态的方式,只有在已授予用户权限的情况下才会要求用户权限。添加else如下:

// Enable if permission granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
        PackageManager.PERMISSION_GRANTED) {
    imageUploader5.setEnabled(true);
} 
// Else ask for permission
else {
    ActivityCompat.requestPermissions(this, new String[]
            { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}

修改

在清单中概括您的user-permission,以便不同和未来的API级别使用它:

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

答案 1 :(得分:3)

defaultConfig {
    applicationId "com.myapp"
    minSdkVersion 11
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

将会这样做。请注意 targetSdkVersion 22

答案 2 :(得分:1)

您检查权限如下

public static int REQUEST_STORAGE_PERMISSION = 122;

 if (checkStoragePermission()) {
      imageUploader5.setEnabled(true);
  } else {
       requestStoragePermission();
  }



 private boolean checkStoragePermission() {
    return ActivityCompat.checkSelfPermission(RegisterActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}

private void requestStoragePermission() {
    ActivityCompat.requestPermissions(RegisterActivity.this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_STORAGE_PERMISSION);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_STORAGE_PERMISSION) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            imageUploader5.setEnabled(true);
        } else {
            imageUploader5.setEnabled(false);

        }
    }
}

答案 3 :(得分:1)

下面的Permission Class链接: -

https://jeeteshsurana.blogspot.com/2017/07/permission-class-pass-activity.html

创建Sperate类并调用

PermissionClass.checkAndRequestPermissions(this);

//如果片段然后将替换为 getActivity()

自动获得许可