requestStoragePermission static method
请求存储权限(可选,应用私有目录不需要)
注意:
- 应用私有目录(getApplicationDocumentsDirectory)不需要权限
- 如果需要导出日志到公共目录(如 Downloads),才需要调用此方法
@return 是否已获得权限
Implementation
static Future<bool> requestStoragePermission() async {
if (kIsWeb) {
debugPrint('[DKLog] Web 平台不需要存储权限');
return true;
}
if (Platform.isIOS) {
// iOS 应用私有目录不需要权限
debugPrint('[DKLog] iOS 应用私有目录不需要权限');
return true;
}
if (Platform.isAndroid) {
// Android 13+ (API 33+) 使用新的权限模型
final androidVersion = await _getAndroidVersion();
if (androidVersion >= 33) {
// Android 13+ 不需要存储权限访问应用私有目录
debugPrint('[DKLog] Android 13+ 应用私有目录不需要权限');
return true;
} else if (androidVersion >= 30) {
// Android 11-12 (API 30-32)
final status = await Permission.storage.status;
if (status.isGranted) {
debugPrint('[DKLog] 存储权限已授予');
return true;
}
final result = await Permission.storage.request();
if (result.isGranted) {
debugPrint('[DKLog] 存储权限请求成功');
return true;
} else if (result.isPermanentlyDenied) {
debugPrint('[DKLog] 存储权限被永久拒绝,请在设置中手动开启');
return false;
} else {
debugPrint('[DKLog] 存储权限被拒绝');
return false;
}
} else {
// Android 10 及以下
final status = await Permission.storage.status;
if (status.isGranted) {
return true;
}
final result = await Permission.storage.request();
return result.isGranted;
}
}
// 其他平台默认允许
return true;
}