Code for check storage permission in android.
Code for check storage permission in android.
173723-Aug-2018
Updated on 02-Jul-2023
Home / DeveloperSection / Forums / Code for check storage permission in android.
Code for check storage permission in android.
Aryan Kumar
02-Jul-2023Sure, here is the code to check storage permission in Android:
public boolean checkStoragePermission() { // Check if the permission has already been granted int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Code snippet
}
This code will first check if the
WRITE_EXTERNAL_STORAGE
permission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will returntrue
. Otherwise, the code will returnfalse
.To use this code, you would need to call the
checkStoragePermission()
method in your app. For example, you could call the method in your app'sonCreate()
method.Here is an example of how you could call the
checkStoragePermission()
method in your app'sonCreate()
method:public void onCreate() { super.onCreate();
Code snippet
Arti Mishra
23-Aug-2018Code for storage permission
public void checkForStoragePermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_STORAGE);
}
} else {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
Toast.makeText(this, "Permission needed to read images", Toast.LENGTH_LONG).show();
}
}