在安卓系统中,MANAGE_EXTERNAL_STORAGE
权限允许应用访问和管理设备上的外部存储,包括SD卡和其他可移除的存储介质。然而,安卓11引入了更严格的存储访问控制,尤其是对于应用私有目录和共享存储的访问。
即使你已经请求并获得了MANAGE_EXTERNAL_STORAGE
权限,安卓11仍然限制了对某些系统文件夹的访问,特别是应用的内部存储空间。这是因为系统希望保护应用的私有数据不被其他应用随意修改。
MANAGE_EXTERNAL_STORAGE
权限。if (ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// 权限已授予
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.MANAGE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
File directory = new File(getFilesDir(), "your_folder_name");
if (directory.exists() && directory.isDirectory()) {
deleteDirectory(directory);
}
private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE);
通过以上方法,你应该能够更好地管理和删除安卓11设备上的文件夹。如果问题依然存在,建议检查是否有其他系统级别的限制或者更新应用到最新版本。
领取专属 10元无门槛券
手把手带您无忧上云