Sure, 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
// If the permission has not been granted, request it
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
return false;
} else {
// The permission has already been granted
return true;
}
}
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 return
true. Otherwise, the code will return false.
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's
onCreate() method.
Here is an example of how you could call the checkStoragePermission() method in your app's
onCreate() method:
public void onCreate() { super.onCreate();
Code snippet
// Check if the storage permission has been granted
if (checkStoragePermission()) {
// The permission has already been granted
} else {
// Request the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, 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_STORAGEpermission 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
Code 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();
}
}