I have this activity that is supposed to access the gallery and choose an image. It run with zero errors but when pressing the "choose " button I got a Permission Denied response in accessing the gallery. What is the problem?
Asked
Active
Viewed 98 times
-1
-
Yes, that is normal. You have to [request permission](https://developer.android.com/training/permissions/requesting) to access Gallery – Md. Asaduzzaman Feb 06 '20 at 11:07
-
Please consider, posting your code :) – alyssaeliyah Feb 06 '20 at 11:10
-
Thanks @AlyssaGono for the edit. I have changed the title to sentence case, as that tends to be preferred here. – halfer Feb 06 '20 at 11:33
-
possible duplicate of https://stackoverflow.com/questions/51374551/read-and-write-external-storage-permission-isnt-working – Ashwini Saini Feb 06 '20 at 11:38
-
maybe be duplicate of https://stackoverflow.com/questions/16360763/permission-denied-when-creating-new-file-on-external-storage – Ashwini Saini Feb 06 '20 at 11:39
-
2Does this answer your question? [Read and Write external storage permission isn't working](https://stackoverflow.com/questions/51374551/read-and-write-external-storage-permission-isnt-working) – Ashwini Saini Feb 06 '20 at 11:40
-
1Hi, Welcome to SO, Please try to search your problem first before asking a question. this question already have answers many time – Ashwini Saini Feb 06 '20 at 11:42
1 Answers
1
If you are target app marshmallow or above you need to add run time permission.
Try below code
Add Permission in your Manifest using below line
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And use below code for get run time permission
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
Permission result callback:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}
For more detail please prefer below link
https://developer.android.com/training/permissions/requesting
I hope this can help you!
Thank You.

Hardik Talaviya
- 1,396
- 5
- 18