SAF实现修改
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import '../models/saf_permission_state.dart';
|
||||
|
||||
/// 使用 file_picker 实现文件夹访问
|
||||
class SafStorageService {
|
||||
static final SafStorageService _instance = SafStorageService._internal();
|
||||
factory SafStorageService() => _instance;
|
||||
SafStorageService._internal();
|
||||
|
||||
static const MethodChannel _channel = MethodChannel(
|
||||
'com.lxh.battery_health/saf',
|
||||
);
|
||||
static const String _prefKeySafPath = 'saf_storage_path';
|
||||
String? _cachedPath;
|
||||
|
||||
@@ -40,14 +42,13 @@ class SafStorageService {
|
||||
|
||||
Future<SafAuthorizationResult> requestPermission() async {
|
||||
try {
|
||||
// 使用 file_picker 选择文件夹
|
||||
String? selectedPath = await FilePicker.platform.getDirectoryPath();
|
||||
final String? selectedPath = await _channel.invokeMethod('selectFolder');
|
||||
|
||||
if (selectedPath == null || selectedPath.isEmpty) {
|
||||
return SafAuthorizationResult.denied();
|
||||
}
|
||||
|
||||
// 验证路径是否包含 mbattery_charger(可选,提高容错)
|
||||
// 验证路径是否包含 mbattery_charger
|
||||
if (!selectedPath.contains('mbattery_charger')) {
|
||||
return SafAuthorizationResult.error('请选择 mbattery_charger 文件夹');
|
||||
}
|
||||
@@ -102,7 +103,7 @@ class SafStorageService {
|
||||
if (!await dir.exists()) return [];
|
||||
final entities = await dir.list().toList();
|
||||
return entities
|
||||
.whereType<File>()
|
||||
.where((e) => e is File)
|
||||
.map((e) => e.path.split(Platform.pathSeparator).last)
|
||||
.toList();
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user