56 lines
1.4 KiB
Dart
56 lines
1.4 KiB
Dart
// lib/utils/file_provider_utils.dart
|
||
import 'dart:io';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter/foundation.dart';
|
||
|
||
class FileProviderUtils {
|
||
static const MethodChannel _channel =
|
||
MethodChannel('com.lxh.qingting_player/file_provider');
|
||
|
||
/// 生成 content:// URI(Android only)
|
||
static Future<Uri?> getContentUri(File file) async {
|
||
if (!Platform.isAndroid) {
|
||
// 非 Android 平台直接返回 file:// URI
|
||
return Uri.file(file.path);
|
||
}
|
||
|
||
try {
|
||
final uriString = await _channel.invokeMethod('getContentUri', {
|
||
'path': file.path,
|
||
});
|
||
if (uriString is String && uriString.isNotEmpty) {
|
||
return Uri.parse(uriString);
|
||
}
|
||
return Uri.file(file.path);
|
||
} on PlatformException catch (e) {
|
||
debugPrint('⚠️ [FileProviderUtils] PlatformException: ${e.message}');
|
||
return Uri.file(file.path);
|
||
} catch (e) {
|
||
debugPrint('⚠️ [FileProviderUtils] Error: $e');
|
||
return Uri.file(file.path);
|
||
}
|
||
}
|
||
|
||
/// 检查文件是否存在
|
||
static Future<bool> fileExists(String path) async {
|
||
try {
|
||
final file = File(path);
|
||
return await file.exists();
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// 删除文件
|
||
static Future<void> deleteFile(String path) async {
|
||
try {
|
||
final file = File(path);
|
||
if (await file.exists()) {
|
||
await file.delete();
|
||
}
|
||
} catch (e) {
|
||
// ignore
|
||
}
|
||
}
|
||
}
|