Files
QingTing-Player/lib/utils/file_provider_utils.dart
T
lxh2875931338 f2b4d1f46d 通知中心封面图显示
部分界面的信息更新
缓存系统初步引入
即将进入播放列表更新
2026-08-25 23:09:17 +08:00

56 lines
1.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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:// URIAndroid 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
}
}
}