Permission Denial: requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() (API 23)

安稳与你 提交于 2019-12-01 08:51:37

By how you structured your if state you will ask for user permissions only if they are already granted. Add an else as follow:

// 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);
}

Edit

Generalize your user-permission in the manifest so it can be used by different and future API levels:

<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" />
defaultConfig {
    applicationId "com.myapp"
    minSdkVersion 11
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

will just do it. note the targetSdkVersion 22

you check permission as below

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);

        }
    }
}

The Permission Class link below:-

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

create Sperate class and call

PermissionClass.checkAndRequestPermissions(this);

// if fragment then replace this to getActivity()

it automatically getting permission

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