SoFunction
Updated on 2025-04-07

Android detection of SD card read and write permissions

1. Analysis

I encountered a difficult problem in doing a project, the read and write permissions of the SD card.

The following code is only useful when the version is 6.0 or above:

if (.SDK_INT >= 23) {
   ().showToast("1");
   //Reduce whether to have permission checkCallPhonePermission != PackageManager.PERMISSION_GRANTED   int checkCallPhonePermission = (getApplicationContext(), permission);
   if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
    ().showToast("2");
    //The dialog box pops up to receive permission    (, new String[]{permission}, id);
    return;
   } else {
    ().showToast("3");
    if(allowableRunnable!=null){
     ();
    }
   }
  }

Because the read and write permissions of the SD card are separated only after Android version 5.0 or above, the previous old versions were not separated. Therefore, the SD card read and write permissions cannot be found. During the testing process, Huawei's mobile phone has SD card read and write permissions separated in two places. Sometimes it may be difficult to find, so I have this problem.

2. Code introduction

Call the first method:

Define two variables before use:

 private Map<Integer, Runnable> allowablePermissionRunnables = new HashMap<>();
 private Map<Integer, Runnable> disallowablePermissionRunnables = new HashMap<>();

1. ".WRITE_EXTERNAL_STORAGE" is the write permission of the SD card. Just check one permission here, and there is no need to detect read permission.

(1) The following first Runnable() is the business that is allowed to be processed later.

(2) The second Runnable() is the business logic executed after the permission prompt box is rejected.

requestPermission(HDCivilizationConstants.SD_CARD_REQUEST_CODE, ".WRITE_EXTERNAL_STORAGE", new Runnable() {
   @Override
   public void run() {
   //The first Runnable    if (type==1){
     (rectBitmap, pathList, 80);
    }else{
     try {
      (rectBitmap, pathList, 70);
     } catch (ContentException e) {
      ();
     }
    }
   }
  }, new Runnable() {
   @Override
   public void run() {
   //The second Runnable// ().showToast("Please check the read and write permissions of the SD card");    ().showPopup(, new () {
     @Override
     public void btnOk() {
      ().dismissDialog();
     }
    }, false, HDCivilizationConstants.SDCARD_PERMISSION);
   }
  });

2. After calling the above method, it will jump to this method (the code is as follows):

 /**
   * Request permission
   * @param id Request the authorized id unique identifier
   * @param permission requested permission
   * @param allowableRunnable
   * @param disallowableRunnable Operation after disabling permissions
   */
 protected void requestPermission(int id, String permission, Runnable allowableRunnable, Runnable disallowableRunnable) {
  if(allowableRunnable!=null){
   (id, allowableRunnable);
  }

  if (disallowableRunnable != null) {
   (id, disallowableRunnable);
  }

  //Api version judgment  if (.SDK_INT &gt;= 23) {
   ().showToast("1");
   //Reduce whether to have permission checkCallPhonePermission != PackageManager.PERMISSION_GRANTED   int checkCallPhonePermission = (getApplicationContext(), permission);
   if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
    ().showToast("2");
    //The dialog box pops up to receive permission    (, new String[]{permission}, id);
    return;
   } else {
    ().showToast("3");
    if(allowableRunnable!=null){
     ();
    }
   }
  } else {
   boolean result = (this, permission)
     == PermissionChecker.PERMISSION_GRANTED;
   if(!result){
    ().showToast("4");
    //If not authorized    (, new String[]{permission}, id);
   }else{
    ().showToast("5");
    if(allowableRunnable!=null){
     ();
    }
   }
  }
 }

The method is executed after responding to the method in 2. After executing the code, start executing the code program in Runnable.

 @Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  (requestCode, permissions, grantResults);

  if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
   Runnable allowRun = (requestCode);
   if(allowRun!=null){
    ();
   }

  } else {
   Runnable disallowRun = (requestCode);
   if(disallowRun!=null){
    ();
   }
  }
 }

The above android detection method for reading and writing permissions of SD cards is all the content I share with you. I hope you can give you a reference and I hope you can support me more.