android Method for Detecting SD Card Read Write Permission
- 2021-09-24 23:39:45
- OfStack
1. Parsing
When doing the project, I encountered a thorny problem, the read-write permission of SD card.
1. The following code is useful when the android version is 6.0 or higher:
if (Build.VERSION.SDK_INT >= 23) {
UiUtils.getInstance().showToast("1");
// Reduce whether you have permissions checkCallPhonePermission != PackageManager.PERMISSION_GRANTED
int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
UiUtils.getInstance().showToast("2");
// Pop-up dialog box to receive permissions
ActivityCompat.requestPermissions(TakeCameraActivity.this, new String[]{permission}, id);
return;
} else {
UiUtils.getInstance().showToast("3");
if(allowableRunnable!=null){
allowableRunnable.run();
}
}
}
Because android version 5.0 above SD card read and write permissions are separated, the previous old version is not separated. Therefore, I can't find the read-write permission of SD card. During the test, Huawei's mobile phone has the read-write permission of SD card separated in two places, which may be difficult to find sometimes, so I have this problem.
2. Code introduction
Call the first method:
Define two variables before using:
private Map<Integer, Runnable> allowablePermissionRunnables = new HashMap<>();
private Map<Integer, Runnable> disallowablePermissionRunnables = new HashMap<>();
1. "android. permission. WRITE_EXTERNAL_STORAGE" This is the write permission of SD card, so it is enough to detect one permission here, instead of detecting the read permission.
(1) The first Runnable () below is the permissions prompt that allows the traffic to be processed later.
(2) The second Runnable () is the business logic executed after the permission prompt is denied.
requestPermission(HDCivilizationConstants.SD_CARD_REQUEST_CODE, "android.permission.WRITE_EXTERNAL_STORAGE", new Runnable() {
@Override
public void run() {
// No. 1 1 A Runnable
if (type==1){
FileUtils.saveBitmapPng(rectBitmap, pathList, 80);
}else{
try {
FileUtils.saveBitmapJPG(rectBitmap, pathList, 70);
} catch (ContentException e) {
e.printStackTrace();
}
}
}
}, new Runnable() {
@Override
public void run() {
// No. 1 2 A Runnable
// UiUtils.getInstance().showToast(" Please view SD Read and write permissions of the card ");
OKPopup.getInstance().showPopup(TakeCameraActivity.this, new OKPopup.BtnClickListener() {
@Override
public void btnOk() {
OKPopup.getInstance().dismissDialog();
}
}, false, HDCivilizationConstants.SDCARD_PERMISSION);
}
});
2. After calling the above method, you will jump to this method (the code is as follows):
/**
* Request permission
* @param id Requesting authorization id Only 1 Just mark
* @param permission Requested permissions
* @param allowableRunnable Actions after agreeing to authorization
* @param disallowableRunnable Operation after disabling permission
*/
protected void requestPermission(int id, String permission, Runnable allowableRunnable, Runnable disallowableRunnable) {
if(allowableRunnable!=null){
allowablePermissionRunnables.put(id, allowableRunnable);
}
if (disallowableRunnable != null) {
disallowablePermissionRunnables.put(id, disallowableRunnable);
}
//api Version judgment
if (Build.VERSION.SDK_INT >= 23) {
UiUtils.getInstance().showToast("1");
// Reduce whether you have permissions checkCallPhonePermission != PackageManager.PERMISSION_GRANTED
int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
UiUtils.getInstance().showToast("2");
// Pop-up dialog box to receive permissions
ActivityCompat.requestPermissions(TakeCameraActivity.this, new String[]{permission}, id);
return;
} else {
UiUtils.getInstance().showToast("3");
if(allowableRunnable!=null){
allowableRunnable.run();
}
}
} else {
boolean result = PermissionChecker.checkSelfPermission(this, permission)
== PermissionChecker.PERMISSION_GRANTED;
if(!result){
UiUtils.getInstance().showToast("4");
// If not authorized
ActivityCompat.requestPermissions(TakeCameraActivity.this, new String[]{permission}, id);
}else{
UiUtils.getInstance().showToast("5");
if(allowableRunnable!=null){
allowableRunnable.run();
}
}
}
}
3. The onRequestPermissionsResult method is executed after the method in response 2. After executing the code, start executing the code program in Runnable.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Runnable allowRun = allowablePermissionRunnables.get(requestCode);
if(allowRun!=null){
allowRun.run();
}
} else {
Runnable disallowRun = disallowablePermissionRunnables.get(requestCode);
if(disallowRun!=null){
disallowRun.run();
}
}
}